I just learning ASP.NET MVC 3 and I'm a bit confused on how I can implement something like what is shown in this link:
http://www.codethinked.com/easy-and-safe-model-binding-in-aspnet-mvc
Why I'm confused is most likely based on my ignorance in the subject. In the example on the page linked above, I see Justin is using a ViewModel that directly contains properties of his "Person" object.
What I want to do is similar, but I want to have a ViewModel actually contain the domain class, which is in my case Company, rather than Person. I'm having the Company class itself implement the ViewModel subset views (ex. IEditCompanyAsUser).
What I've tried so far looks something like so:
public class Company : IValidatableObject, ICompanyUpdateAsUser
{
[Key]
[ScaffoldColumn(false)]
public virtual int CompanyID { get; set; }
[Required]
public virtual Boolean Active { get; set; }
[Required]
public virtual string Name { get; set; }
[Required]
public virtual string Phone { get; set; }
// Containing some more members below
}
With ICompanyUpdateAsUser containing the properties I would like my controller's Edit method to update:
public interface ICompanyUpdateAsUser
{
bool Active { get; set; }
string Name { get; set; }
string Phone { get; set; }
}
And a ViewModel class that looks like so:
public class CompanyViewModel
{
public Company Company;
// Will be adding more members in the future
}
What I can't figure out is how to properly code the controller. My controller currently looks like so:
/// <summary>
/// POST: /Company/Edit/5
/// </summary>
[HttpPost]
public ActionResult Edit([Bind(Prefix="Company")]Company model)
{
var company = _companyService.GetCompany(model.CompanyID);
if (company == null)
return RedirectToAction("Index");
if (ModelState.IsValid)
{
if (TryUpdateModel<ICompanyUpdateAsUser>(company))
{
_companyService.SaveCompany();
return RedirectToAction("Details", new { companyId = model.CompanyID });
}
}
return View(model);
}
I know something is absolutely wrong with my controller code but I can't figure out what components I need to use to implement such a pattern? Do I need to use a custom model binder? Do I need to use some other feature of ASP.NET MVC3 to implement the pattern?
Any advice would be greatly appreciated.
Thanks!
*EDIT*
What I need to do is have the Edit view post back a "Company" object, then update the object from the database with the values on the post'ed Company (according to the properties on the interface passed to TryUpdateModel). Everything appears to be working (validation, etc.), but the TryUpdateModel(company) does not seem to be working, though evaluating to true. When I step through the code with a debugger, the Company object pulled from the DB is simply never updated.
After a bit more reading, I've determined the TryUpdateModel method updates the passed item from the method's IValueProvider. How can I associated the passed in "Company model" with the IValueProvider for the method?