1
我通過一些虛擬數據我的看法正好有一些工作提出:Html.DisplayFor上沒有返回值用它在MVC 4 Razor視圖
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);
}
但是,當我調試這個我看是t他Description
屬性發回,但Title
屬性沒有價值。正如你在上面看到的,我用@Html.DisplayFor(x => x[i].Title)
顯示它們。這是一些默認行爲還是我做錯了什麼?
感謝形式的一部分稱號。也許真的不需要跟蹤只讀值。要重新考慮我想要什麼。但很高興知道'@ Html.DisplayFor'是如何工作的。 – Leron 2013-05-05 19:36:18