ASP.NET MVC 5模型编辑器返回空模型
问题描述:
我有这样的代码: 型号ASP.NET MVC 5模型编辑器返回空模型
public class Matrix
{
public ValidInt[][] Data;
public int Width { get; set; }
public int Height { get; set; }
public Matrix(int w, int h)
{
Width = w;
Height = h;
Data = new ValidInt[w][];
for (int i = 0; i < w; i++)
this.Data[i] = new ValidInt[h];
}
public class ValidInt
{
[Range(0, 8, ErrorMessage = "Enter proper number")]
public int Value { get; set; }
}
}
然后,我创建新的Matrix并把它传递给视图:
[HttpGet]
public ActionResult Fill(int w, int h)
{
Matrix matr = new Matrix(h, w);
return View(matr);
}
[HttpPost]
public ActionResult Fill(Matrix matr) {...}
鉴于我使用
@model Mondrian.Models.Matrix
...
@using (Html.BeginForm("Fill", "Picture", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@for (int column = 0; column < Model.Data.Length; column++)
{
<tr>
@for (int row = 0; row < Model.Data[column].Length; row++)
{
<td>@Html.EditorFor(x => Model.Data[column][row].Value)</td>
}
</tr>
}
</table>
<button type="submit">Submit</button>
}
但是,当我按下提交按钮,它进入我的POST方法,但通过矩阵参数是空的,并使用默认的构造函数创建。我只想通过Matrix来查看,然后从用户获取数据,发布它并使用输入的数据。我究竟做错了什么?
答
好,解决了加入
{get;set}
我的数据属性。
视图中可以共享'Form'声明吗?还有提交表单的提交操作方法? –
注意:你对不同的属性有同样的错误信息 –
请给出'HttpPost'的动作方法。那里发生了什么? – ironstone13