Suppose I have the following:
Action:
public ActionResult Details(int id)
{
Person person = personRepository.GetPerson(id);
return View(person)
}
This is my view:
Name: <%= Html开发者_JAVA技巧.Encode(Model.Name) %><br />
Age: <%= Html.Encode(Model.Age) %><br />
Birth Date: <%= Html.Encode(Model.Birthday) %><br />
Country: <%= Html.Encode(Model.Country) %><br />
Weight: <%= Html.Encode(Model.Weight) %><br />
Height: <%= Html.Encode(Model.Height) %><br />
Phone: <%= Html.Encode(Model.PhoneNumber) %>
My issues is that country, weight, and height are nullable. How can I do something like this
foreach (var field in Model)
if (var field != null)
{
field.name + ": " + field.value
}
I can do it like this but i'm sure there is a better way:
if (Model.Name != null){
<%= Html.Encode(Model.Name)%>
}
Thank you in advance
Country:<%: string.IsNullOrEmpty(Model.Country) ? String.Empty : Model.Country %>
<br />
note that : In <%: %> MVC 2 you can use it for Html.Encode
精彩评论