开发者

Exclude properties on model binding using interfaces

This is an example I have lifted here: http://aspalliance.com/1776_ASPNET_MVC_Beta_Released.5

public ActionResult Save(int id)
{
 Person person = GetPersonFromDatabase(id);
 try
 {
  UpdateMode<IPersonFormBindable>(person)
  SavePersonToDatabase(person);

  return RedirectToAction("Browse");
 }
 catch
 {
  return View(pers开发者_开发知识库on)
 }
}

interface IPersonFormBindable
{
 string Name  {get; set;}
 int Age   {get; set;}
 string Email {get; set;}
}

public class Person : IBindable
{
 public string Name   {get; set;}
 public int Age    {get; set;}
 public string Email {get; set;}
 public Decimal? Salary  {get; set;}
}

This will not map values to property Salary but will execute its validation attributes which is not expected when you do the standard [Bind(Exclude="Salary")]

[Bind(Exclude="Salary")]
public class Person
{
 public string Name  {get; set;}
 public int Age   {get; set;}
 public stiring Email {get; set;}
 public Decimal? Salary  {get; set;}
}

How will I implement the [Bind(Exclude="Property")] using this interface pattern?


Here's what I would recommend you: use view models, and drop those interfaces, don't clutter your controllers with so much code. So you don't need the salary to be bound in this specific action: great, use a specifically tailored view model to this view:

public class PersonViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}

And your controller action:

public ActionResult Save(PersonViewModel person)
{
    _repository.SavePersonToDatabase(person);
    return RedirectToAction("Browse");

    // Notice how I've explicitly dropped the try/catch,
    // you weren't doing anything meaningful with the exception anyways.
    // I would simply leave the exception propagate. If there's 
    // a problem with the database it would be better to redirect 
    // the user to a 500.htm page telling him that something went wrong.
}

If in another action you needed the salary then write another view model specific to this view. Don't worry if you repeat some of the properties between your view models. That's exactly what they are meant for. Obviously your repository will work with a model and not view model. So you will need to map between those two types. I would recommend you using AutoMapper for this purpose.

That's my point of view: always write view models specifically tailored to the needs of a given view. Try to avoid those Include, Exclude or one day it will bite you, someone is going to add some sensitive property and will forget to add it to the Exclude list. And even if you used Include it is still ugly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜