【问题标题】:MVC3 drop down list confusionMVC3 下拉列表混淆
【发布时间】:2011-08-19 08:23:19
【问题描述】:

我正在使用带有 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]
public ActionResult Edit(Article article)
{
    if (ModelState.IsValid)
    {
        db.Entry(article).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(article);
}

我遇到的问题是,当 HttpPost Edit 返回时,article.Section 为空。如何强制视图将 Section 绑定到正在编辑的文章。

任何帮助表示赞赏。

【问题讨论】:

    标签: entity-framework asp.net-mvc-3 binding drop-down-menu


    【解决方案1】:

    不要将模型直接推送到视图。请改用 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);
    }
    

    希望对你有帮助

    【讨论】:

    • 那行得通 - 谢谢。尽管只是绑定一个关联对象,但似乎需要做很多工作。想知道是否有更简单的方法。
    • 您实际上可以直接在没有 Foreach 的情况下创建一个 SelectList,方法是这样做: SelectList oTest = new SelectList(YourListOfObjects, "ValuePropertyOfTheObject", "TextPropertyOfTheObject");然后使用下拉列表中的 SelectList。
    猜你喜欢
    • 2012-05-29
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多