ASP.NET MVC3:DropdownList中的对象在控制器中为空

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

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

@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; }
}

参考

  • 在mvc3编辑表单中下拉菜单

  • 如何在包含可为null的枚举属性的模型的强类型视图中使用Html.DropDownList?

  • MVC3 HTML助手不会在提交表单时更新DropdownListFor

  • 在选择更改事件 - Html.DropDownListFor

  • MVC3 @ Html.DropDownListFor不填充选定的项目


  • @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


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

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

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

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

    链接地址: http://www.djcxy.com/p/48125.html

    上一篇: ASP.NET MVC3: Object from DropdownList in Null in Controller

    下一篇: Can you overload controller methods in ASP.NET MVC?