Created
September 14, 2011 12:36
-
-
Save moniyax/1216449 to your computer and use it in GitHub Desktop.
Selective Model Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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