我将一些虚拟数据传递给我的视图只是为了有一些东西可以使用:
public ActionResult Index()
{
ViewBag.Message = "Create table";
var model = new List<Auction>();
model.Add(new Auction
{
Title = "First Title",
Description = "First Description"
});
model.Add(new Auction
{
Title = "Second Title",
Description = "Second Description"
});
model.Add(new Auction
{
Title = "Third Title",
Description = "Third Description"
});
model.Add(new Auction
{
Title = "Fourht Title",
Description = "Fourth Description"
});
return View(model);
}
我在我的视图中显示这个:
@model List<Ebuy.Website.Models.Auction>
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
<table border="1" >
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.HiddenFor(x => x[i].Id)
@Html.DisplayFor(x => x[i].Title)
</td>
<td>
@Html.EditorFor(x => x[i].Description)
</td>
</tr>
}
</table>
<button type="submit">Save</button>
}
现在我想对提交的数据进行一些操作:
[HttpPost]
public ActionResult Index(List<Auction> model)
{
var test = model;
model[1].Title = "Test";
return View(model);
}
但是当我调试它时,我看到Description
属性被发回,但Title
属性没有价值。正如你在上面看到的,我用@Html.DisplayFor(x => x[i].Title)
. 这是一些默认行为还是我做错了什么?