2011-09-07 54 views
0

我正在使用pubs數據庫,基本上我在一個頁面上顯示了作者列表,並且有編輯鏈接。在點擊編輯鏈接時,除了特定作者所寫的標題外,還會顯示一個頁面,其中包含作者信息和帶複選標記的複選框列表。數據未採取行動方法綁定

這是我的代碼至今:

的HomeController action方法:

public ActionResult Edit(string id) 
{ 
    author author = db.authors.Include("titleauthors").Where(a => a.au_id == id).Single(); 

    List<TitleViewModel> titleViewModelList = db.titles.Select<title, TitleViewModel>(title => 
     new TitleViewModel { IsChecked = false, Title = title.title1, title_id = title.title_id }).ToList(); 

    foreach (var item in author.titleauthors) 
    { 
     TitleViewModel titleViewModel = titleViewModelList.Where(model => model.title_id == item.title_id).SingleOrDefault(); 
     if (titleViewModel != null) 
      titleViewModel.IsChecked = true; 
    } 

    AuthorViewModel AuthorViewModel = new AuthorViewModel 
    { 
     Author = author, 
     TitleViewModels = titleViewModelList 
    }; 

    return View(AuthorViewModel); 
} 

[HttpPost] 
public ActionResult Edit(AuthorViewModel AuthorViewModel) 
{ 

    return RedirectToAction("Index"); 
} 

Edit.cshtml:

@model MVCCheckboxList.ViewModels.AuthorViewModel 
@{ 
    ViewBag.Title = "Edit"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2> 
    Edit</h2> 
<fieldset> 
    @using (Html.BeginForm()) 
    { 
     <legend>Edit author</legend> 
     <table> 
      <tr> 
       <td> 
        @Html.LabelFor(model => model.Author.au_fname) 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.Author.au_fname) 
       </td> 
      </tr> 
      <tr> 
       <td> 
        @Html.LabelFor(model => model.Author.au_lname) 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.Author.au_lname) 
       </td> 
      </tr> 
      <tr> 
       <td> 
        @Html.LabelFor(model => model.Author.address) 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.Author.address) 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        Titles 
       </td> 
       <td> 
        @Html.EditorFor(model => model.TitleViewModels, "TitleViewModels") 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <p> 
         <input type="submit" value="Save" /> 
        </p> 
       </td> 
      </tr> 
     </table> 
    } 
</fieldset> 

這是我AuthorViewModel:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MVCCheckboxList.ViewModels 
{ 
    public class AuthorViewModel 
    { 
     public List<TitleViewModel> TitleViewModels { get; set; } 
     public author Author { get; set; } 
    } 
} 

和這是我的TitleViewModel:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MVCCheckboxList.ViewModels 
{ 
    public class TitleViewModel 
    { 
     public string title_id { get; set; } 
     public bool IsChecked { get; set; } 
     public string Title { get; set; } 
    } 
} 

信息在編輯頁面中正確顯示。但是,當我點擊提交時,AuthorViewModel中的Author屬性正確地保存了所有數據,但中的List<TitleViewModel>爲空。任何人都可以發現錯誤,爲什麼會發生這種情況?

這是我EditorTemplate(TitleViewModels.cshtml):

@model IEnumerable<MVCCheckboxList.ViewModels.TitleViewModel> 
<table> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.HiddenFor(modelItem => item.title_id) 
     </td> 
     <td> 
      @Html.CheckBoxFor(modelItem => item.IsChecked) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Title) 
     </td> 
    </tr> 
} 

</table> 

回答

2

Here你找到列表綁定更多的信息。

只要改變你的EditorTemplate的名稱TitleViewModel.cshtml和粘貼代碼:

@model MVCCheckboxList.ViewModels.TitleViewModel 

<td> 
    @Html.HiddenFor(modelItem => Model.title_id) 
</td> 
<td> 
    @Html.CheckBoxFor(modelItem => Model.IsChecked) 
</td> 
<td> 
    @Html.DisplayFor(modelItem => Model.Title) 
</td> 

在下面的代碼Edit.cshtml使用從您的列表生成的每個元素編輯

<table> 
@for (int i = 0; i < Model.TitleViewModels.Count; i++) { 
    @Html.EditorFor(m => Model.TitleViewModels[i]) 
} 
</table> 
+0

我想你說的話但這不起作用。我收到異常「Visual Studio 2010 \ Projects \ MVCCheckboxList \ MVCCheckboxList \ Views \ Shared \ EditorTemplates \ TitleViewModel.cshtml(3):error CS0103:名稱'item'在當前上下文中不存在' – Jaggu

+0

好的對不起!有用。你問我粘貼代碼,所以我粘貼沒有使用常識。 @ Html.HiddenFor(modelItem => item.title_id)必須是@ Html.HiddenFor(modelItem => modelItem .title_id);)。謝謝krolik – Jaggu

+0

對不起,我編輯了我的答案(使用'Model'或'modelItem'代替'item') – krolik

相關問題