2017-03-01 77 views
1

我無法將模型綁定到Controller。請給我任何建議。 模型,控制器和視圖類如下。當模型submmited時,字典屬性等於null。Dictionary <short,Dictionary <EnFunction,bool >> model binding not working

public class GroupRights //model 
{ 
    public List<DtoGrup> groups { get; set; } 
    public Dictionary<short, Dictionary<EnFunction, bool>> groupRights { get; set; } // group function HasPermission 
} 

public enum EnFunction 
{ 
    LPDU_login, 
    LPDU_changePassword, 
    LPDU_transitList, 
    LPDU_PosEventList, 
    .... 
} 

控制器

public ActionResult GroupRights() 
{ 
    TocCommonService.CommonServiceClient client = new TocCommonService.CommonServiceClient(); 
    GroupRights gr = new GroupRights(); 
    gr.groups = client.GetAllOperatorGroups().ToList(); 
    gr.groupRights = new Dictionary<short, Dictionary<EnFunction, bool>>(); 
    foreach (var g in gr.groups) 
    { 
     Dictionary<EnFunction, bool> permission = new Dictionary<EnFunction, bool>(); 
     foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>()) 
     { 
      permission.Add(func, client.hasPermission(new DtoGrup() { GROUPID = g.GROUPID }, func));     
     } 
     gr.groupRights.Add(g.GROUPID, permission); 
    } 
    return View(gr); 
} 

查看

@model TocWebApplication.Models.GroupRights 
@{ 
    int id = 0; 
} 
@using (Html.BeginForm("ChangePermissionOfGroup", "Home", FormMethod.Post)) 
{ 
    <table> 
     <thead> 
      <tr> 
       <th></th> 
       @foreach (var gr in Model.groups) 
       { 
        <th>@gr.GROUPNAME (@gr.GROUPID)</th> 
       } 
      </tr> 
     </thead> 
     <tbody> 
      @foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>()) 
      { 
       <tr> 
        <td>@(func.ToString())</td> 
        @for (int j = 0; j < Model.groups.Count(); j++) 
        { 
         <td>@Html.CheckBoxFor(model => model.groupRights[Model.groups[j].GROUPID][func])</td> 
        } 
       </tr> 
      } 
     </tbody> 
    </table> 

    <button type="submit" class="btn btn-primary">Save changes</button> 
    <button class="btn">Cancel</button> 
} 
+0

似乎你的問題是與[字典綁定](http://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary) –

+0

我搜索了所有帖子在stackoverflow,但我didn' t解決問題:( –

回答

1

對於所有但字典其中兩個所述KeyValue是簡單值類型(例如Dictionary<string, bool>),則DefaultModelBinder要求名稱/值屬性的格式(在你的情況下)

<input name="groupRights[0].Key" value="1" ... /> 
<input name="groupRights[0].Value[0].Key" value="LPDU_login" ... /> 
<input name="groupRights[0].Value[0].Value" value="True" ... /> 
<input name="groupRights[1].Key" value="2" ... /> 
<input name="groupRights[1].Value[0].Key" value="LPDU_changePassword" ... /> 
<input name="groupRights[1].Value[0].Value" value="False" ... /> 

有沒有HtmlHelper方法可以生成正確的屬性,並綁定到您需要手動生成html的groupRights

相反,創建一個表示要在view.If顯示的數據視圖模型(S)我已經正確地解釋這一點,你想顯示顯示每個EnFunction值下表,每個DtoGrup對面,和在每個單元格中顯示一個複選框,以確定爲每個DtoGrup選擇了哪個EnFunction

public class GroupRightsVM // represents a table cell 
{ 
    public short GroupID { get; set; } 
    public bool IsSelected { get; set; } 
} 
public class RightsVM // represents a table row 
{ 
    public RightsVM() 
    { 
     Groups = new List<GroupRightsVM>() 
    } 
    public EnFunction Right { get; set; } 
    public List<GroupRightsVM> Groups { get; set; } // represents the cells in a row 
} 
public class PermissionsVM // Represents the table 
{ 
    public PermissionsVM() 
    { 
     Permissions = new List<RightsVM>() 
    } 
    public IEnumerable<string> Headings { get; set; } // represents the table headings 
    public List<RightsVM> Permissions { get; set; } // represents all rows 
} 

並在視圖

<thead> 
    <tr> 
     <th></th> 
     @foreach(var heading in Model.Headings) 
     { 
      <th>@heading</th> 
     } 
    </tr> 
</thead> 
<tbody> 
    @for(int i = 0; i < Model.Rows.Count; i++) 
    { 
     <tr> 
      <td> 
       @Html.HiddenFor(m => m.Permissions[i].Right) 
       @Html.DisplayFor(m => m.Permissions[i].Right) 
      </td> 
      @for(int j = 0; j < Model.Permissions[i].Groups.Count; j++) 
      { 
       <td> 
        @Html.HiddenFor(m => m.Permissions[i].Groups[j].GroupID) 
        @Html.CheckboxFor(m => m.Permissions[i].Groups[j].IsSelected) 
       </td> 
      } 
     </tr> 
    } 
</tbody> 

在GET方法,初始化的PermissionsVM的實例,填充它的基礎上您的數據模型,並把它傳遞給視圖,例如

var groups = client.GetAllOperatorGroups() 
var model = new PermissionsVM(); 
model.Headings = groups.Select(x => String.Format("{0} {{1})", x.GROUPNAME, x.GROUPID)); 
foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>()) 
{ 
    RightsVM r = new RightsVM(); 
    r.Right = func; 
    foreach (var g in groups) 
    { 
     GroupRightsVM gr = new GroupRightsVM(); 
     gr.GroupID = g.GROUPID; 
     gr.IsSelected = ... 
     r.Groups.Add(gr); 
    } 
    model.Persmissions.Add(r); 
} 
return View(model); 

並在POST方法中

public ActionResult GroupRights(PermissionsVM model) 
+0

作爲一個側面說明,我爲另一個用戶創建的[DotNetFiddle](https://dotnetfiddle.net/NWRMQM)是一個類似的場景。 –

+0

非常感謝Stephen –

相關問題