ASP.NET MVC3:来自控制器中Null的DropdownList的对象

时间:2012-02-18 09:32:07

标签: c# asp.net asp.net-mvc asp.net-mvc-3 html-helper

我为BookShop创建了一个示例MVC3应用程序。我有一个创建动作。在创建操作中,用户输入书名,作者姓名并选择流派名称。它没有错误地工作。但是在控制器中,我没有得到所选的流派名称( - 流派对象为null)。我们如何纠正它?

注意:当我编写@Html.DropDownList("GenreId", String.Empty) ASP.NET MVC内置的模型绑定功能时,将尝试使用表单输入填充该类型的对象。例如。它会尝试设置book对象的GenreId值。但Book不包含属性GenreId。但是,下拉列表可以通过从ViewBag中读取所需的值来显示值。

CODE

@model MyBookShopApp.Book

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {

@Html.ValidationSummary(true)

<fieldset>

    <legend>BOOK</legend>


    <div >
        @Html.LabelFor(model => model.Title) :~: @Html.EditorFor(model => model.Title)
    </div>


    <div >
        @Html.LabelFor(model => model.Artist.Name )  :*: @Html.EditorFor(model => model.Artist.Name)
    </div>


    <div >
        @Html.LabelFor(model => model.GenreEntity.Name, "The Genre") ::   @Html.DropDownList("GenreId", String.Empty)
    </div>


    <p>
        <input type="submit" value="Create" />
    </p>

</fieldset>
}

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

CONTROLLER

namespace MyBookShopApp.Controllers
{
public class StoreManagerController : Controller
{

    List<GenreEntity> listOfGenres = new List<GenreEntity>()
                                    {
                                       new GenreEntity { Name = "Fiction", GenreId = 1 },
                                       new GenreEntity { Name = "Science", GenreId = 2 },
                                       new GenreEntity { Name = "Religious", GenreId = 3 },
                                    };

    List<Book> bookList = new List<Book>()
                          {
                            new Book
                            {
                                BookId = 1,Title = "Book1",
                                GenreEntity = new GenreEntity { Name = "Fiction", GenreId = 1 },
                                Artist = new Artist { ArtistId = 1, Name = "Dinesh" }
                            },
                            new Book
                            {
                                BookId = 2,Title = "Book2",
                                GenreEntity = new GenreEntity { Name = "Science", GenreId = 2 },
                                Artist = new Artist { ArtistId = 1, Name = "Lijo" }
                            },
                            new Book
                            {
                                BookId = 3,Title = "Book3",
                                GenreEntity = new GenreEntity { Name = "Religious", GenreId = 3 },
                                Artist = new Artist { ArtistId = 1, Name = "Santhosh" }
                            }

                          };





    #region CREATE



    // GET: 
    public ActionResult Create()
    {
                    ViewBag.GenreId = new SelectList(listOfGenres, "GenreId", "Name");
        return View();
    }

    // POST:
    [HttpPost]
    public ActionResult Create(Book theBook)
    {
        if (ModelState.IsValid)
        {
            //Save the book in DB first and then redirectToAction.
            return RedirectToAction("Index");
        }

        return View(theBook);
    }

    #endregion


}
}

书类

public class Book
{
    public int BookId { get; set; } 
    public string Title { get; set; }
    public GenreEntity GenreEntity { get; set; }
    public Artist Artist { get; set; }

}

GenreEntity

  public class GenreEntity
{
    public string Name { get; set; }
    public int GenreId { get; set; }
}

艺术家班

 public class Artist
{
    public int ArtistId { get; set; }
    public string Name { get; set; }
}

参考

  1. dropdown in mvc3 edit form

  2. How to use a Html.DropDownList in a strongly-typed view for a model containing a nullable enum property?

  3. MVC3 HTML helper doesn't update DropdownListFor on Submit the form

  4. on select change event - Html.DropDownListFor

  5. MVC3 @Html.DropDownListFor not populating selected item

2 个答案:

答案 0 :(得分:2)

@Html.DropDownList("GenreId", String.Empty)创建一个名为GenreId的SELECT。但是,MVC3期望名称GenreEntity.Name绑定到Book。

简单的解决方案是将public ActionResult Create(Book theBook) 修改为public ActionResult Create(Book theBook, string genreId)以接收已发回的genreId

<强>替代地

@Html.LabelFor(model => model.GenreEntity.GenreId, "The Genre")
@Html.DropDownListFor(model => model.GenreEntity.GenreId, (SelectList)ViewBag.GenreId, String.Empty)

请记住在HttpPost Create方法中设置ViewBag。

修改 已将属性名称从BookId更正为GenreId

答案 1 :(得分:1)

您的DropDown正在尝试将其值分配给名为Book.GenreId的属性。您的GenreEntity类具有GenreId属性,而不是Book(基于您上面粘贴的代码)。

您可以通过修改此下拉列表来更正此问题:

@Html.DropDownList("GenreEntity.GenreId", String.Empty)

这将告诉MVC将下拉值分配给Book.GenreEntity.GenreId