System.MissingMethodException: there is no constructor without parameters for the object.

I am using MVC 2.0 with Html.ListBoxFor as shown below:

<% using (Html.BeginForm()) { %> <input type="submit" value=">" /> <%= Html.ListBoxFor(x => x.lstTest, new MultiSelectList(new [] {"someone", "crap", "why"})) %> <% } %> 

When I press the enter input button below, nothing is selected, it returns a message in the order, when I select one of the 3 items in the list, it gives this error:

  System.MissingMethodException: No parameterless constructor defined for this object. 

Any ideas? here is my controller code:

  [HandleError] public class HomeController : Controller { public HomeController() { } public ActionResult Index() { ViewData["Message"] = "Test Harness"; return View(); } [HttpGet] public ActionResult About() { ViewData["mykey"] = "Test Harness"; LogOnModel model = new LogOnModel(); model.lstTest = new MultiSelectList(new [] {"A", "B", "C"}); return View(model); } [HttpPost] public ActionResult About(LogOnModel model) { ViewData["mykey"] = "Test Harness"; model.lstTest = new MultiSelectList(new [] { "" }); return View(model); } } 
+1
source share
2 answers

Does your LogOnModel have a constructor without parameters? One instance is required to create an instance of DefaultModelBinder. Also, when you post an exception, send a full stack trace from the exception object, otherwise we just guess where the error actually occurred.

+4
source

This error comes from the ControllerFactory, not from the view. It states that you do not have a paramerless constructor in your controller. DefaultControllerFactory for ASP.NET MVC can only instantiate a controller with an open constructor with a lower value. If you plug in your own ControllerFactory and use some DI / IoC tool, you can get around this limitation.

0
source

Source: https://habr.com/ru/post/911615/


All Articles