2013-01-04 39 views
0

我對MVC相當陌生,所以我確信我做錯了什麼。我正在盡我所能去做所有的MVC方式,而不是恢復到只用HTML手工製作。如何使用列表創建ViewModel的編輯視圖

這裏是我的編輯,查看

@model NotesBoard.Models.RoleViewModel 

@{ 
    ViewBag.Title = "Edit"; 
} 

<h2>Edit</h2> 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>RoleViewModel</legend> 
    @Html.HiddenFor(model => model.Role.Name) 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Role.Name) 
    </div> 
    <div class="editor-field"> 
     @Html.DisplayFor(model => model.Role.Name) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Role.Users) 
    </div> 
    <div class="editor-field"> 
     <table> 
      <tr> 
       <td class="remove-td">Tick to Remove</td> 
       <td></td> 
       <td class="add-td">Tick to Add</td> 
      </tr> 
      <tr> 
       <td style="vertical-align:top; margin: 0px;"> 
        <table style="margin: 0px;"> 
        @foreach (var item in Model.Role.Users) 
        { 
         <tr> 
          <td class="remove-td"> 
           @Html.CheckBoxFor(modelItem => item.State) 
          </td> 
          <td> 
           @Html.DisplayFor(modelItem => item.User) 
          </td> 
         </tr> 
        } 
        </table> 
       </td> 
       <td></td> 
       <td style="vertical-align:top; margin:0px;"> 
        <table style="margin: 0px;"> 
        @foreach (var item in Model.Users) 
        { 
         Boolean RoleUser = Model.Role.Users.Exists(model => model.User == item.User); 
         <tr> 
         @if(RoleUser) 
         { 
          <td class="add-td"> 
           @Html.CheckBoxFor(modelItem => item.State, new { disabled=true }) 
          </td> 
          <td class="disabled-label"> 
           @Html.DisplayFor(modelItem => item.User) 
          </td> 
         } 
         else 
         { 
          <td class="add-td"> 
           @Html.CheckBoxFor(modelItem => item.State, new { id=item.User }) 
          </td> 
          <td> 
           @Html.DisplayFor(midelItem => item.User) 
          </td> 
         } 
         </tr> 
        } 
        </table> 
       </td> 
      </tr> 
     </table> 
    </div> 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

這裏是我的模型:

public class AccountUser 
{ 
    public AccountUser() { } 

    public AccountUser(Int32 id, String user) 
    { 
     Id = id; 
     User = user; 
    } 

    [Key] 
    public Int32 Id { get; set; } 
    public String User { get; set; } 
} 

public class AccountUserViewModel : AccountUser 
{ 
    public AccountUserViewModel(Int32 id, String user, Boolean State = false) 
    { 
     Id = id; 
     User = user; 
    } 

    public Boolean State { get; set; } 
} 

public class RoleModel 
{ 
    public String Name { get; set; } 
    public List<AccountUserViewModel> Users { get; set; } 
} 

public class RoleViewModel 
{ 
    public RoleModel Role { get; set; } 
    public List<AccountUserViewModel> Users { get; set; } 
} 

當我從編輯視圖回發兩個用戶列表爲空。

這裏是Post方法在我的控制器至今:

[HttpPost] 
    [Authorize(Roles = "Admin")] 
    public ActionResult Edit(RoleViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var data = Request.Form; 
      return RedirectToAction("Index"); 
     } 
     return View(model); 
    } 

如果我嘗試直接通過使用Request.Form集合中提取數據,該複選框都被命名爲「item.state」。我應該如何實現這個視圖來獲取血統數據,而不會恢復到基本上只是自己製作平面舊HTML的表單?

回答

1

更改foreach通過列表來FOR環路,使得索引可以在LAMDA被使用並且由Razor視圖正確呈現:

   @for (int i = 0; i < Model.Users.Count(); i++) { 

       { 

        <tr> 
        @if(RoleUser) 
        { 
         <td class="add-td"> 
          @Html.CheckBoxFor(modelItem => Model.Users[i].State, new { disabled = true }) 
         </td> 
         <td class="disabled-label"> 
          @Html.DisplayFor(modelItem => Model.Users[i].User) 
         </td> 
        ... 
        ... 

模型粘合劑將然後正確地將數據綁定到列表。

看看這個回答:https://stackoverflow.com/a/6585642/1099260

+0

這很好用。我想補充一點,我必須確保我必須爲我的AccountUserViewModel類添加一個parameterles構造函數,併爲Id和User字段添加了隱藏字段,否則您只會返回索引,這在我看來並不可靠。謝謝 – Gineer

相關問題