The closest question I found in StackOverflow to what I have is Posting data when my view model has a constructor does not work
Model
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
ViewModel
public class CustomerViewModel
{
public Customer Customer { get; set; }
public CustomerViewModel(Customer customer)
{
Customer = customer;
}
}
Controller Code
public ActionResult CreateCustomer()
{
Customer c = new Customer();
CustomerViewModel cvm = new CustomerViewModel(c);
return View(cvm);
}
[HttpPost]
public ActionResult CreateCustomer(CustomerViewModel customer)
{
// do something here
}
View Code
@model Blah.Models.CustomerViewModel
@{
ViewBag.Title = "CreateCustomer";
}
<h2>CreateCustomer</h2>
@using (Html.BeginForm())
{
<div class="editor-label">
@Html.LabelFor(model => model.Customer.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.FirstName)
@Html.ValidationMessageFor(model => model.Customer.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Customer.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.LastName)
@Html.ValidationMessageFor(model => model.Customer.LastName)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
Error
Solutions that just get rid of the error but not helpful
- Adding a Default Constructor (parameter is empty - doesn't serve my purpose)
- Dont have the overloaded constructor (My Model will be empty then)
Question
I guess I need a custom model binder here. Don't know how to create one :-(
(or)
I would like to know what other options I have here