I'm working on a generic Details page in MVC 3 that looks like this in the View:
<fieldset>
<legend style="font-size:large">Info</legend>
<div class="display-label">Url1:</div>
<div class="display-field">
@Html.DisplayFor(model => model.Url1)
</div>
<div class="display-label">Url2:</div>
<div class="display-field">
@Html.DisplayFor(model => model.Url2)
</div>
<fieldset>
and like this in the Model:
public HtmlString Url1
{
get
{
if (!string.IsNullOrEmpty(Url1))
{
return new HtmlString("<a href=\"" + Url1
+ "\" target=\"_blank\">" + Url1 + "</a>");
}
return new HtmlString("<b>no url</b>");
}
}
public HtmlString Url2
{
get
{
if (!string.IsNullOrEmpty(Url2))
{
return new HtmlString("<a href=\"" + Url2
+ "\" target=\"_blank\">" + Url2 + "</a>");
}
return new HtmlString("<b>no url</b>");
}
}
With Url1 being a url with the form http://www.website.com, and Url2 having the form www.website.org/file.aspx
When I debug my code the HtmlString for Url1 displays correctly, but the HtmlString for Url2 does not. The resulting Html source looks like:
<div class="display-label">Url1</div>
<div class="display-field">
<a href="http://www.website.org">website</a> <br/>
</div>
<div class="display-label">Url2</div>
<div class="display-field">
</div>
Has anyone experienced issues like this before, and if so how did you fix them?
Update
Updated code to use @Model.Url1 instead of @Html.DisplayFor(Url1). Problem solved.
@Html.DisplayFor()when you can just do@model.Url2?