1

i have the following model

public class Person
{
    public int Id {get;set;}
    [Required()]
    public string Name {get;set;}
    [Required()]
    public Address Address {get;set;}
}

public class Address
{
    public int Id {get;set;}
    [Required()]
    public string City {get;set;}
    [Required()]
    public string Street {get;set;}
}

in the controller:

 [HttpPost]
        public ActionResult Create(Person entity)
        {
            if (ViewData.ModelState.IsValid)
            {
                ///Some code
                return this.RedirectToAction("Browse");
            }
            else
            {
                return View("Edit", ViewModel);
            }
        }

the problem is that the binder try to validate even the inner address class, but all i care for, is the AddressID but the ModelBinder insist to validate even the City and Street properties.

how can i simply override the original ModelBinder just to validate the ID of the inner object (which is in my situation is AddressID)??

is there a simple way ?

animuson
  • 53,861
  • 28
  • 137
  • 147
Nour
  • 5,252
  • 3
  • 41
  • 66

1 Answers1

1

It sounds like your entity and your model have two different requirements. If that is the case, then they should be two different classes. Write a separate Person and address class for MVC to bind to and don't have city or street be required.

Another possible, but less elegant solution, is to not rely on MVC doing the model binding. If you only have a handful of values that may be acceptable, but if you have a lot, then I would use my first suggestion.

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • well, may be the example i wrote in the question is too simple, but the situation is more complex than that, let's say i have an order object and a customer, when i save the order, all the database care for (in order to save the Order) is just the CustomerID, not his name nor his martial status. so when the user fill an order, he just pick the customer, and we just take his ID. that is why i don't want the Mvc to validate the whole customer object. – Nour Dec 14 '10 at 23:23
  • 1
    In that case I would go with my first suggestion, have a separate class that MVC binds to, then fill in your data entities after all of the validation has been performed. – Brian Ball Dec 14 '10 at 23:28
  • or may be i can override the way MVC bind models, and make it bind the ID of the Complex object only ?? – Nour Dec 15 '10 at 09:37
  • You would need to override the validation, not the binding. Given MVC huge push towards separations of concerns, I'm sure it's completely possible, though I don't know how to do it off the top of my head. If possible, I would only make it have that behaviour where you needed it and not make it a global standard in your application. Another option would be to output the select list yourself, give it a name that MVC won't be able to bind to the object model, then pull the value out of the request. If there are no properties to bind to in a complex model, I believe it leaves it null. – Brian Ball Dec 15 '10 at 13:06
  • the reason why i want to modify the way mvc binds the model, is that when it binds the person object, it also bind all the properties in the address object, and after mvc binds it, it will validate all the binded properties, so i thought if i prevent the mvc from bind all the Address properties, it will be fine, i'm not sure if it is the correct way, could you please guide me, how to override the way Mvc validate properties ? – Nour Dec 15 '10 at 15:42
  • This looks like a good resource on how model validation works and where the extension points are: http://dotnetslackers.com/articles/aspnet/Customizing-ASP-NET-MVC-2-Metadata-and-Validation.aspx#s2-validation – Brian Ball Dec 15 '10 at 15:57