2017-09-25 105 views
0

我有這樣的代碼: 型號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來查看,然後從用戶獲取數據,發佈它並使用輸入的數據。我究竟做錯了什麼?

+0

視圖中可以共享'Form'聲明嗎?還有提交表單的提交操作方法? –

+0

注意:你對不同的屬性有同樣的錯誤信息 –

+0

請給出'HttpPost'的動作方法。那裏發生了什麼? – ironstone13

回答

0

好,解決了加入

{get;set} 

我的數據屬性。