ASP.NET MVC: No parameterless constructor defined for this object
I just had a similar problem. The same exception occurs when a Model
has no parameterless constructor.
The call stack was figuring a method responsible for creating a new instance of a model.
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
Here is a sample:
public class MyController : Controller
{
public ActionResult Action(MyModel model)
{
}
}
public class MyModel
{
public MyModel(IHelper helper) // MVC cannot call that
{
// ...
}
public MyModel() // MVC can call that
{
}
}
MVC: No parameterless constructor defined for this object
When you create your controller it needs in the constructor a parameter, called IAccommodationService
.
If you don't, you can't create the controller.
One way to solve the problem is to use Dependency Injection (DI)/ Inversion of Control (IoC) like ninject or similar.
If you have one, then remember to register the IAccommodationService
...
MVC ASP.NET No parameterless constructor defined for this object
it's because you don't have parameterless constructor in your ViewModel class. You have 2 constructors there:
- public NewFixedAccountViewModel(string username)
- public NewFixedAccountViewModel(int accountID, string username)
When you submit the form, during model binding, the controller is trying to create a new instance of the model, but it doesn't find parameterless constructor.
So you need to have:
public NewFixedAccountViewModel() {
// Your code here
}
What you can do is maybe use the Username property instead of username parameter, so your code will be:
public NewFixedAccountViewModel()
{
AccountTypes = new SelectList(
ManageAccountType.Instance.getTypes(), "id", "type", _Account.typeID);
AccountFromList = new SelectList(
ManageAccount.Instance.GetUserAccounts(Username), "accountID", "name", accountFrom);
Currencies = new SelectList(
ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);
Durations = new SelectList(
ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);
}
MVC:No parameterless constructor defined for this object
You need to include below constructor to your controller,
public HomeController() : this(new CompanyService())
{
}
So your entire controller code looks like below,
public class HomeController : Controller
{
private ICompanyService icompanyService;
public HomeController() : this(new CompanyService())
{
}
public HomeController(ICompanyService icompanyService)
{
this.icompanyService = icompanyService;
}
public ActionResult Index()
{
ViewBag.CompanyName = this.icompanyService.GetCompany();
return View();
}
}
This will solve your issue.
Happy coding!!!!!
No parameterless constructor defined for this object in mvc4
activator trying to create ur controller with parametrless constructor. but while your controller constructor has parameter
public ClaimAuditAdminController(IClaimsAuditAdminRepository claimAuditAdminRepository) <--this place
{
this.claimAuditAdminRepository = claimAuditAdminRepository;
}
the exception occurs.
Try to remove param from constructor or try override the DefaultControllerFactory
and create controller with parameter.
look here
No parameterless constructor defined for this object for MVC Controller
mvc needs parameterless contructions.
If you have no constructor, .Net-runtime create parameterless contruction automaticly, otherwise if you have a contruction with parameter, than the runtime don't do that and raise an error.
Ether you create a new carService on each controler, or you use a serviceProvider.
The unittest are simple with a serviceprovider.
With asp.net core its easy enter link description here, if you use mvc 4 or 5. you can see here a see here a sample enter link description here
No parameterless constructor defined for this object error after implementing Unit of Work
The main issue was "dependency injection".
private readonly IUnitOfWork uow;
PopulationsController(IUnitOfWork uow)
{
this.uow = uow;
}
I installed Unity.Mvc5 from Nuget Packages. I has in-built configuration to make dependency injections.
After adding Unity.Mvc5. It created UnityConfig.cs file in App_start automatically.
1)Add "container.RegisterType<Interface, Repository>();" in UnityConfig.cs
2)Add "UnityConfig.RegisterComponents();" in Global.asax.cs.
Related Topics
C# VS Java Enum (For Those New to C#)
When to Use Cast() and Oftype() in Linq
How to Rethrow Innerexception Without Losing Stack Trace in C#
Generics in C#, Using Type of a Variable as Parameter
How to Tell a Lambda Function to Capture a Copy Instead of a Reference in C#
JSON.Net Throws Stackoverflowexception When Using [JSONconvert()]
How to Decompile a .Net Exe into Readable C# Source Code
ASP.NET MVC Custom Error Handling Application_Error Global.Asax
How to Bind a List to a Combobox
Get a Screenshot of a Specific Application
How to Know If a Process Is Running