2011-08-19 104 views
0

我正在使用MVC3與EF 4.1,並試圖編輯一個模型,它有一個下拉列表,這是一個父對象的引用。下面是型號:MVC3下拉列表混淆

public class Section 
{ 
    public Guid SectionId { get; set; } 
    public string Title { get; set; } 
    public virtual ICollection<Article> Articles { get; set; } 
} 

public class Article 
{ 
    public Guid ArticleId { get; set; } 
    public DateTime? DatePosted { get; set; } 
    public string Title { get; set; } 
    public string ArticleBody { get; set; } 
    public Section Section { get; set; }   
} 

這裏的控制器動作來渲染編輯的GET部分:

public ActionResult Edit(Guid id) 
{ 
    Article article = db.Articles.Find(id); 
    var sections = db.Sections.ToList(); 
    var secIndex = sections.IndexOf(article.Section); 
    ViewBag.SectionId = new SelectList(sections, "SectionId", "Title", secIndex);    
    return View(article); 
} 

和視圖

@model CollstreamWebsite.Models.Article 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Article</legend> 
     @Html.HiddenFor(model => model.ArticleId) 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.DatePosted) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.DatePosted) 
      @Html.ValidationMessageFor(model => model.DatePosted) 
     </div> 

     ... 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Section) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("SectionId") 
     </div> 

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

終於爲編輯POST操作

​​

我遇到的問題是,當HttpPost編輯回來時,article.Section爲空。如何強制視圖將該部分綁定到正在編輯的文章。

任何幫助表示讚賞。

回答

4

不要將您的模型直接推入您的視圖。改用ViewModel。

事情是這樣的:

視圖模型

public class EditArticleViewModel 
{ 

    ///All the properties for your Article 
    ///The SelectListItems for your Sections 

    public List<SelectListItem> Sections{ get; set; } 
    public String SelectedSection{ get; set; } 


} 

編輯獲取

[HttpGet] 
public ActionResult Edit(Guid id)  
{ 

    EditArticleViewModel oEditArticleViewModel = new EditArticleViewModel(); 

    //Fill in the SelectLists 
    List<SelectListItem> Sections= new List<SelectListItem>(); 
    Sections.Add(new SelectListItem() { Text = "TheSelectedSection", Value = SectionId.ToString(), Selected = true});  

    foreach(Section otherSection in AllPossibleSections) 
    { 
     Sections.Add(new SelectListItem() { Text = otherSection.Title, Value = otherSection.Id, Selected = false}); 
     } 

     oEditArticleViewModel.Sections = Sections; 


    return View(oEditArticleViewModel); 
} 

你查看

@Html.DropDownListFor(model => model.SelectedSection, Model.Sections) 
//All other needed properties with their textboxes etc. 

編輯帖子

[HttpPost] 
public ActionResult Register(EditArticleViewModel oPostedViewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     //Get the Article and fill in the new properties etc. 
     //You can get the selectedSection from the SelectedSection Property, just cast it to a Guid. 


     RedirectToAction("Index", "Home"); 
    }    

    //Something went wrong, redisplay the form for correction. 
    //Make sure to fill in the SelectListItems again. 
    return View(oPostedViewModel); 
} 

希望它可以幫助

+0

這工作 - 謝謝。看起來很多工作,只是爲了綁定一個關聯對象。想知道是否有更簡單的方法。 – Matt

+0

你可以直接在沒有Foreach的情況下直接創建一個SelectList: SelectList oTest = new SelectList(YourListOfObjects,「ValuePropertyOfTheObject」,「TextPropertyOfTheObject」);然後在下拉列表中使用SelectList。 –