ASP.NET MVC: No Parameterless Constructor Defined for This Object

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



Leave a reply



Submit