Reputation: 25
I am new to MVC3 and after 2 days of reading, I cannot find what I am doing wrong. Here is the scenario:
I have a strongly typed view:
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/NestedMasterPage.Master"
Inherits="System.Web.Mvc.ViewPage<AdminToolMVC.Models.PageModels.MembershipVM>" %>
<% using (Html.BeginForm("UpdateUser", "Admin"))
{ %>
<%: Html.CheckBoxFor(m => m.Tab6VM.Programs[0].Selected,
new { id = "prog1" })%>
<%: Model.Tab6VM.Programs[0].Description %></br>
<%: Html.CheckBoxFor(m => m.Tab6VM.Programs[1].Selected,
new { id = "prog2" })%>
<%: Model.Tab6VM.Programs[1].Description %></br>
<%: Html.CheckBoxFor(m => m.Tab6VM.Programs[2].Selected,
new { id = "prog3" })%>
<%: Model.Tab6VM.Programs[2].Description %></br>
<%: Html.CheckBoxFor(m => m.Tab6VM.Programs[3].Selected,
new { id = "prog4" })%>
<%: Model.Tab6VM.Programs[3].Description %></br>
<input type="submit" id="btnUpdate" value="Update" />
<%} %>
In my admin controller:
public ActionResult Index()
{
//return some view
}
[HttpPost]
public ActionResult UpdateUser(MembershipVM pageModel)
{
UserAdminDW dataWriter;
Models.PageModels.MembershipVM =
new Models.PageModels.MembershipViewModel();
try
{
model = StateHelper.MEMBERSHIP;
}
catch (Exception e)
{
return RedirectToAction("SessionExpired", "Error");
}
}
I was under the impression that my strongly typed view page should send the the data that the user is changing inside the form to the UpdateUser method in my AdminController class.
But instead, I am getting the "no parameterless constructor defined for this object." error.
I tried to add a parameter to the controller call from within the view, but the page model that comes through to the controller is null. All I want to do is after displaying the user data on the page, enable the user to update some of the information and send it back to the controller, so I can save it in the database. What am I missing? I am not using Razor engine, only the classic MVC3. Thanks for the help.
Upvotes: 0
Views: 773
Reputation: 4541
The error occurs when the model binder are trying to populate an object(view model) and it cannot find a public/parameterless constructor(oh rly?).
Example
ViewModel
public class CustomerViewModel {
public CustomerViewModel() { // <- public constructor without params.
Created = DateTime.Now;
}
public Guid CustomerId { get; set; }
public DateTime Created { get; set; }
public AddressViewModel Address { get; set; }
}
public class AddressViewModel {
public AddressViewModel(Guid customerId) { // <- BOOM! AddressViewModel have none parameterless constructor, so it can't be created
CustomerId = customerId;
}
public Guid CustomerId { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
public CustomerViewModel Customer { get; set; }
}
Action
[HttpPost]
public ActionResult UpdateCustomer(CustomerViewModel customer) {
// when modelbinder are trying to create the 'customer', it sees that CustomerViewModel have an AddressViewModel(which have none parameterless constructor) property, and you know what happen.
}
Hope it helps.
Upvotes: 2