开发者

ASP.NET MVC : Remove Empty fields in Details View

开发者 https://www.devze.com 2023-02-21 12:50 出处:网络
Suppose I have the following: Action: public ActionResult Details(int id) { Person person = personRepository.GetPerson(id);

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号