我有以下情况(Asp.Net MVC 5):
在我的ViewModel中,我有类似的东西
public partial class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ActivityID {get; set; }
public IEnumerable<Activity> Activities {get; set; }
}
控制器:
public ActionResult Edit()
{
var model = db.Person.Where(q => q.Status == 0).ToList();
return View(model);
}
(剃刀)视图
@model IEnumerable<Models.PersonViewModel>
<!-- some html code here -->
@{
int i = 0;
foreach(var item in Model)
{
<td>@Html.TextBoxFor(item => item.FirstName, new {@class = "form-control"})</td>
<td><input type="text" name="PersonViewModel[@i].LastName" class="form-control" /></td>
i++;
}
}
<!-- some html code here -->
在上面的视图中,未生成TextBoxFor。所以我用这样的for循环尝试了它:
for(int i = 0; i < Model.Count(); i++)
{
<td>@Html.TextBoxFor(model => model.[i].FirstName, new { @class="form-control" })
}
但在这种情况下,无法选择FirstName属性。在做的时候:
@Html.TextBoxFor(model => model[i].FirstName, new {@class="form-control"})
无法找到属性FirstName。我做错了什么/我不明白?