使用HTML.TextBoxFor遍历模型中的项目 [英] Using HTML.TextBoxFor to loop through items in a model
问题描述
可能是一个愚蠢的问题,但是我是MVC的新手.
到目前为止,在剃刀中我可以说@HTML.TextBoxFor(t => t.EmailAddress)
但现在我有一个for-each:
foreach(var q in Model.Questions)
{
// so here the t => t.EmailAddress syntax is not working anymore.
}
我在上面的代码示例中问了我的问题.因此,当我处于for-each循环内时,如何使用@HTML.TextBox
?因为现在它不再使用lambda语法了.
请勿使用foreach
,因为当您尝试将输入绑定回模型时,这将导致问题.而是使用for
循环:
for (var i = 0; i < Model.Questions.Count(); i++) {
@Html.TextBoxFor(m => m.Questions[i])
}
另请参见模型绑定到列表MVC 4 ./p>
Probably a stupid question but I am new to MVC.
So far in my Razor I could say @HTML.TextBoxFor(t => t.EmailAddress)
but now I have a for-each:
foreach(var q in Model.Questions)
{
// so here the t => t.EmailAddress syntax is not working anymore.
}
I asked my question in the code sample above. So when I am inside a for-each loop how can I can use @HTML.TextBox
? because now it doesn't get the lambda syntax anymore.
Do not use foreach
, because this will cause problems when you try to bind your inputs back to the model. Instead use a for
loop:
for (var i = 0; i < Model.Questions.Count(); i++) {
@Html.TextBoxFor(m => m.Questions[i])
}
See also Model Binding to a List MVC 4.
这篇关于使用HTML.TextBoxFor遍历模型中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!