Asp.net
MVC ViewModel 錯誤 - 沒有為此對象定義無參數建構子
我想知道如何在 Create Action 上使用我的 ViewModel?我嘗試了幾個在論壇中找到的範例,但沒有一個能解決我的問題。這幾天我一直在絞盡腦汁,但不知道出了什麼問題。
每當我點擊創建按鈕時,我都會收到以下錯誤:沒有為此對象定義無參數建構子。
@model MvcMusicStore.ViewModels.AlbumViewModel @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Album</legend> <div class="editor-label"> @Html.LabelFor(model => model.AlbumItem.GenreId, "Genre") </div> <div class="editor-field"> @Html.DropDownList("Genres", String.Empty) @Html.ValidationMessageFor(model => model.AlbumItem.GenreId) </div> <div class="editor-label"> @Html.LabelFor(model => model.AlbumItem.ArtistId, "Artist") </div> <div class="editor-field"> @Html.DropDownList("Artists", String.Empty) @Html.ValidationMessageFor(model => model.AlbumItem.ArtistId) </div> <div class="editor-label"> @Html.LabelFor(model => model.AlbumItem.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.AlbumItem.Title) @Html.ValidationMessageFor(model => model.AlbumItem.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model.AlbumItem.Price) </div> <div class="editor-field"> @Html.EditorFor(model => model.AlbumItem.Price) @Html.ValidationMessageFor(model => model.AlbumItem.Price) </div> <div class="editor-label"> @Html.LabelFor(model => model.AlbumItem.AlbumArtUrl) </div> <div class="editor-field"> @Html.EditorFor(model => model.AlbumItem.AlbumArtUrl) @Html.ValidationMessageFor(model => model.AlbumItem.AlbumArtUrl) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>創建.cshtml
public class StoreManagerController : Controller { private MusicStoreDB db = new MusicStoreDB(); // // GET: /StoreManager/Create public ActionResult Create() { var viewModel = new AlbumViewModel() { Genres = new SelectList(db.Genres, "GenreId", "Name"), Artists = new SelectList(db.Artists, "ArtistId", "Name") }; return View(viewModel); } // // POST: /StoreManager/Create [HttpPost] public ActionResult Create(AlbumViewModel vm) { if (ModelState.IsValid) { db.Albums.Add(vm.AlbumItem); db.SaveChanges(); return RedirectToAction("Index"); } vm.Genres = new SelectList(db.Genres, "GenreId", "Name", vm.AlbumItem.GenreId); vm.Artists = new SelectList(db.Artists, "ArtistId", "Name", vm.AlbumItem.ArtistId); return View(vm); } }StoreManager.cs - 片段
public class AlbumViewModel { public AlbumViewModel() { // nothing } public Album AlbumItem { get; set; } public SelectList Genres { get; set; } public SelectList Artists { get; set; } } public class Album { public Album() { // nothing } public virtual int AlbumId { get; set; } public virtual int GenreId { get; set; } public virtual int ArtistId { get; set; } public virtual string Title { get; set; } public virtual decimal Price { get; set; } public virtual string AlbumArtUrl { get; set; } public virtual Genre Genre { get; set; } public virtual Artist Artist { get; set; } } public class Artist { public Artist() { // nothing } public virtual int ArtistId { get; set; } public virtual string Name { get; set; } } public class Genre { public Genre() { // nothing } public virtual int GenreId { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual List<Album> Albums { get; set; } }
如果我每次看到這個問題都有一個鎳幣。它通常與模型屬性的命名以及如何在
DropDownList. 99.999% 的時間是因為人們使用Html.DropDownList()和命名它與他們的SelectList. 這是您應該使用強類型的原因之一DropDownListFor。在這種情況下,您的問題是您有
SelectLists 命名Genres和Artists,那麼在您看來,您有:@Html.DropDownList("Genres", String.Empty) @Html.DropDownList("Artists", String.Empty)看,同名。
您應該做的是更改您的模型以使
SelectLists 被命名為GenreListandArtistList。然後,更改您的視圖以使用強類型模型。@Html.DropDownListFor(m => m.AlbumItem.GenreID, Model.GenreList) @Html.DropDownListFor(m => m.AlbumItem.ArtistID, Model.ArtistList)發生這種情況的原因是您正在向控制器發布一個名為 Genres 的值。預設模型綁定器盡職盡責地在模型中查找名為 Genres 的內容並將其實例化。但是,它找到了一個名為 Genres 的 SelectList,而不是一個 ID 或字元串,當它嘗試實例化它時,它發現沒有預設建構子。
因此你的錯誤。SO充滿了關於同樣事情的問題。