在视图中循环浏览模型并为每个属性创建文本框 [英] Loop through model and create textbox for each property, in the view

查看:89
本文介绍了在视图中循环浏览模型并为每个属性创建文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有5-6个属性的模型.不必繁琐地编写一行代码在视图中创建文本框,而是可以遍历模型中的所有属性(公共属性)并为每次迭代在视图中创建文本框?

I have a model with 5-6 properties. Rather than tediously writing a line of code to create a textbox in the view, is it possible to loop through all the properties in the model (which are public) and create a textbox in the view for each iteration?

谢谢

推荐答案

是的,您可以使用@Html.EditorForModel(),或者,如果您想进行更精细的控制,则可以借助一些反思来生成文本框:

Yes you can use @Html.EditorForModel() or if you want more granular control you can generate the textboxes with the help of a little reflection:

@foreach (var property in Model.GetType().GetProperties())
{
   @Html.Label(property.Name)
   @Html.TextBox(property.Name)
}

这篇关于在视图中循环浏览模型并为每个属性创建文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆