ASP.NET MVC:删除详细信息视图中的空字段
假设我有以下内容:
行动:
public ActionResult Details(int id)
{
Person person = personRepository.GetPerson(id);
return View(person)
}
这是我的观点:
Name: <%= Html.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) %>
我的问题是国家/地区、体重和身高可为空。我怎样才能做这样的事情
foreach (var field in Model)
if (var field != null)
{
field.name + ": " + field.value
}
我可以这样做,但我确信有更好的方法:
if (Model.Name != null){
<%= Html.Encode(Model.Name)%>
}
提前谢谢
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.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意 : 在 <%: %> 中MVC 2 你可以将它用于 Html.Encode
note that : In <%: %> MVC 2 you can use it for Html.Encode