Skip to content

Instantly share code, notes, and snippets.

@moniyax
Created September 14, 2011 12:36
Show Gist options
  • Save moniyax/1216449 to your computer and use it in GitHub Desktop.
Save moniyax/1216449 to your computer and use it in GitHub Desktop.
Selective Model Binding
interface ICreatePerson
{
string Name { get; set; }
string Sex { get; set; }
int Age { get; set; }
}
interface IUpdatePerson
{
string Name { get; set; }
}
class Person : ICreatePerson, IUpdatePerson
{
public int Id { get; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
public ActionResult Edit(int id, FormCollection collection)
{
// Get orig person from db
var person = this.personService.Get(id);
try
{
// Update person from web form
UpdateModel<IUpdatePerson>(person);
// Save person to db
this.personService.Update(person);
return RedirectToAction("Index");
}
catch
{
ModelState.AddModelErrors((person.GetRuleViolations());
return View(person);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment