ASP.NET MVC3:控制器中 Null 中 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>
控制器
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; }
}
参考:
I have created a sample MVC3 application for BookShop. I have a create action. In the create action users enter name of a book, author name and select a genre name. It is working without errors. But in the controller, I am not getting the selected genre name ( - the genre object comes as null). How do we correct it?
Note: When I write @Html.DropDownList("GenreId", String.Empty)
the Model Binding capabilities built into ASP.NET MVC, will attempt to populate an object of that type using form inputs. E.g. it will try to set the book object’s GenreId value. But Book does not contain the property GenreId. However the dropdown can show the values by reading required values from 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
}
}
Book Class
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; }
}
Artist Class
public class Artist
{
public int ArtistId { get; set; }
public string Name { get; set; }
}
REFERENCE:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
@Html.DropDownList("GenreId", String.Empty)
创建一个名为 GenreId 的 SELECT。然而,MVC3 期望名称 GenreEntity.Name 绑定到 Book。简单的解决方案是修改
public ActionResult Create(Book theBook)
到public ActionResult Create(Book theBook, string GenreId)
来接收发回的 GenreId或者
请记住在 HttpPost Create 方法中也设置您的 ViewBag。
编辑
将属性名称从 BookId 更正为 GenreId
@Html.DropDownList("GenreId", String.Empty)
creates a SELECT with the name GenreId. However MVC3 is expecting the name GenreEntity.Name to bind to Book.The simple solution is to modify
public ActionResult Create(Book theBook)
topublic ActionResult Create(Book theBook, string genreId)
to receive the genreId that got posted backAlternatively
Remember to set your ViewBag inside the HttpPost Create method also.
Edit
Corrected the property name from BookId to GenreId