MVC ViewModel error - no constructor without parameters was specified for this object

I would like to know how can I use my ViewModel to create an action? I tried a few examples that I found here on the forum, but no one solved my problem. I’ve been racking my brains for several days, but I can’t understand what’s wrong.

Whenever I click the Create button, I get the following error: A constructor without parameters is not defined for this object.

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

Create.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 - Snippet

 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; } } 
+8
asp.net-mvc asp.net-mvc-3 razor
source share
3 answers

If I had nickel for every time, I saw this problem. This is usually related to the name of the model properties and how you use them in DropDownList . 99.999% of the time, because people use the Html.DropDownList() and call it the same as their SelectList . This is one of the reasons you should use the strongly typed DropDownListFor .

In this case, your problem is that you have a SelectList named Genres and Artists , then in your view you have:

 @Html.DropDownList("Genres", String.Empty) @Html.DropDownList("Artists", String.Empty) 

See the same name.

What you have to do is change your model to make the SelectList named GenreList and ArtistList . Then change your look to use a strongly typed model.

 @Html.DropDownListFor(m => m.AlbumItem.GenreID, Model.GenreList) @Html.DropDownListFor(m => m.AlbumItem.ArtistID, Model.ArtistList) 

The reason for this is because you are sending a value called Genres to the controller. By default, the model binder dutifully looks in the model to find something called "Genres" and create an instance. But instead of an identifier or a string, he finds a SelectList named Genres, and when he tries to create it, he discovers that there is no default constructor.

So your mistake. SO is filled with questions asking about the same.

+24
source share

Like Eric Funkhenbush’s Answer I added a DropDownList to my form, however in my case it was not (and was not intended) to submit with the form, since it was outside the <form></form> tags:

 @Html.DropDownList("myField", Model.MyField) 

Since the model contains only a field for display, this also caused a No parameterless constructor defined for this object error because the field was not sent at all.

In this case, I fixed it by adding an exclude binding:

 public ActionResult Foo(int id, int? page, [Bind(Exclude = "MyField")]MyModel model) 
0
source share

For me, the problem was in the BeginForm () method itself. It looked like this:

 @using (Html.BeginForm("MyAccount", "MyController", Model)) 

Copied and pasted from another project login page that does not have a drop-down list.

In any case, remove the model from the parameter list, and all this works fine :)

0
source share

All Articles