2015-03-13 98 views
1

我有一個MVC5項目,我試圖將「權限」列表發佈回服務器。向控制器發佈模型陣列

@model List<ConnectConsole.Models.Permissions> 

<form action="/products/@{@Html.Raw(Url.RequestContext.RouteData.Values["id"])}/permissions/save" method="POST"> 
    @for (int i = 0; i < Model.Count; i++) 
    { 
     @Html.TextBoxFor(model => model[i].ConnectId) 

     @Html.DropDownListFor(
      model => model[i].ScopeVal, 
      Model[i].ScopeTypes, 
      "-- Please Select --")       
    } 
    <button class="btn btn-full btn-submit">Save</button> 
    @* removed some html for brevity *@ 
</form> 

被回發到服務器的數據看起來像這樣的財產以後(從鉻複製)

[0].ConnectId:88709d85-710c-45da-8fec-0ee661d267b1 
[0].ScopeVal:e276964f-7e6e-4aaf-8bf5-cac5b02f3a8f 
[1].ConnectId:78709d85-710c-45da-8fec-0ee661d267b1 
[1].ScopeVal: 
[2].ConnectId:68709d85-710c-45da-8fec-0ee661d267b1 
[2].ScopeVal: 

我的C#模型

public class Permissions 
{ 
    public String ConnectId { get; set; } 
    public List<SelectListItem> ScopeTypes; 
    public String ScopeVal; 
} 
,我張貼到

方法簽名

public ActionResult Save(string id, List<Permissions> permissions) 

T他的對象似乎正確地發佈到服務器與所有的請求中的所有字段,但在該方法從該數據序列化的列表只包含連接ID,它是正確的大小,我得到一個列表有3它的對象,但是ScopeVal從未設置。

這是MVC中的錯誤還是我在做可怕的錯誤?

回答

1

MVC默認的模型綁定器只會綁定屬性與公共getter和setter。

您需要更改領域

public String ScopeVal; 

是一個財產

public String ScopeVal { get; set; } 

這是不好的C#揭露類字段 - 這是什麼性質對。

+0

是的,這是確切的問題,幾乎感覺愚蠢的錯過:/ – Daxxy 2015-03-13 09:20:33

+0

@Daxxy我不會擔心 - 模型活頁夾是一個黑魔法! – 2015-03-13 09:28:25

相關問題