Transforming bank information into Link

Asked

Viewed 89 times

3

I’m making a site with a string from the database but I want this string to be informed as follows, in place of it will appear a button that the user will click and this button will open the url that is the text that ta in the string that in the case is the Link to Download.

<dt>
                @Html.DisplayNameFor(model => model.Link)
            </dt>
            <dd>
                @Html.DisplayFor(modelItem => item.Link)
            </dd>

Here I bring the string item to appear on the screen, but I want it to be a button that opens the URL that is itself.

  • There’s an example of what it’s like string? she comes around like this: http://meusite.com.br/alguma_coisa or /something

2 answers

0


You can use the component called Html.ValueFor, it returns the value contained in the named attribute within the href, and then stylize the <a>(if you want you can use my css). That is, assuming that your string is a link of the same type "www.google.com", just put this in your View by swapping "Click here" for the text of your button.

<a class="button-style" href="@Html.ValueFor(m => m.Link)">Clique aqui</a>

CSS style for button:

.button-style {
    background-color: #4CAF50; /* Green */
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}
  • I did the test and when I clicked the button this link opened: http://localhost:58611/Items/www.globo.com , in the case the String is www.globo.com

  • When you give a Debug.Writeline the string is www.globo.com ? certainty?

  • @Romãogomes how did your code get there? it looks like he’s riding a Html.ActionLink

  • yes, send me your email to send you the project please.

  • @Romãogomes no need, so put a breakpoint in your controller and print the model to see what value is in it.

0

The simplest way to do it is to use tag <a> passing your url coming in the model to the attribute href. You can also use Bootstrap to style your button.

 <a href="http://@modelItem.Link" class="btn btn-primary">Site</a>

<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>

<a href="http://www.google.com" class="btn btn-primary">Site</a>

If you want to use Razor you can create your own helper.

public static class LinkHelper
{
    public static string ExternalLink(this HtmlHelper helper, string url, string text)
    {
        return String.Format("<a href='http://{0}' target="_blank"  class="btn btn-primary">{1}</a>", url, text);
    }
}

Using the Help.

@Html.ExternalLink("www.google.com", "Google")

Source: https://www.codeproject.com/Questions/864248/How-can-I-add-links-to-external-web-sites-in-mvc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.