您可以使用基于两个视图模型的复杂模型来创建表单或使用基于数组的简单模型。
这是可以做什么的非常简单的示例(如果需要,我可以使用两个 ViewModel 制作更复杂的示例)。
视图模型:
public class TestViewModel
{
public int[][] Matrix { get; set; }
public TestViewModel()
{
}
public TestViewModel(string i)
{
Matrix = new int[4][] { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 } };
}
}
控制器:
public ActionResult Test()
{
return View(new TestViewModel("sa"));
}
[HttpPost]
public ActionResult Test(TestViewModel model)
{
//some business logic goes here
//ViewModel to Model convertion goes here
return View(model);
}
看法:
@model TestViewModel
@using (Html.BeginForm())
{
<div class="form_block">
@for (int i = 0; i<Model.Matrix.Length;i++)
{
for (int j = 0; j<Model.Matrix[i].Length;j++)
{
@Html.TextBoxFor(x=>x.Matrix[i][j])
}
}
</div>
<div class="submit_block">
<input type="submit" value="Сохранить изменения" />
</div>
}