ASP.NET MVC3:控制器中DropdownList中的对象为Null
本文关键字:对象 Null DropdownList ASP MVC3 控制器 NET | 更新日期: 2023-09-27 18:24:58
我为BookShop创建了一个示例MVC3应用程序。我有一个创造动作。在创建动作中,用户输入书籍名称、作者名称并选择流派名称。它工作时没有出错。但在控制器中,我没有得到所选的流派名称(-流派对象为null)。我们如何纠正它?
注意:当我编写@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; }
}
参考:
mvc3编辑表单中的下拉列表
如何在包含可为null枚举属性的模型的强类型视图中使用Html.DropDownList?
MVC3 HTML帮助程序没有';t在提交表单时更新DropdownListFor
关于选择更改事件-Html.DropDownListFor
MVC3@Html.DropDownListFor not populating selected item
@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
。