我將使用視圖模型建議您:
public class MyViewModel
{
public string Id { get; set; }
public string Text { get; set; }
}
,然後在你的控制器動作,你會發送這些模型視圖列表:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new MyViewModel { Id = "1", Text = "blah" },
new MyViewModel { Id = "2", Text = "blahblah" },
new MyViewModel { Id = "3", Text = "blahblah" },
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
// Here you will get a collection of id and text for each item
...
}
}
,你可以在視圖使用的ID隱藏字段和值文本框:
@model IEnumerable<MyViewModel>
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
和correspondin g編輯器模板(~/Views/Shared/EditorTemplates/MyViewModel.cshtml
):
@model MyViewModel
@Html.HiddenFor(x => x.Id)
@Html.TextBoxFor(x => x.Text)