1

I have this in MVC3 Razor.. I want to make this a hyperlink..so users can click on it. Right now it just prints the text of the URL in the view.. thanks for your help.

   <td>
        @Html.DisplayFor(modelItem => item.WebLink)
    </td>

Thanks to Richard, heres the solution..

  @{
       var link = @item.WebLink;
       if (link != null)
       {
           if (!link.StartsWith("http://"))
            { 
               link = "http://"+link;
            }
       }
     }

        <a href="@link">@item.WebLink</a>

4 Answers 4

5

You should try:

<a href="@item.WebLink">@item.WebLink</a>

OK from you comment you will need this:

@{
   var link = @item.WebLink;
   if (!link.StartsWith("http://")) { link = "http://"+link; }
}

<a href="@link">@item.WebLink</a>
Sign up to request clarification or add additional context in comments.

3 Comments

But the click is not taking me to the google URL.. its actually taking me to localhost:2323/Controller/ActionName/www.google.com.. anything I am missing?
Ok fixed now for the http issue
You are missing the paranthesis.. I modified a little bit.. this works best. Thank you for showing me the right way to do this..I edited the original post also.. @{ var link = @item.WebLink; if (link != null) { if (!link.StartsWith("http://")) { link = "http://"+link; } } } <a href="@link">@item.WebLink</a>
1

You can simply use:

<td><a href="@item.WebLink">@item.WebLink</a></td>

Assuming item is the variable name within your for loop.

1 Comment

But the click is not taking me to the google URL.. its actually taking me to localhost:2323/Controller/ActionName/www.google.com.. anything I am missing? – ZVenue
1

This works for me:

@{
    string site = "www.google.com";
}

<a href="http://@site"> Go to the site @site </a>

Comments

0

conver it into

simple html

<a href="@item.WebLink">Linkname</a>

if you want it in razor

@html.actionlink("@item.WebLink","actionname","controllername")

i dont knw in which format ur link will come but if it is in home/index jst split it using split("/") and use it in razor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.