How [and where] to implement validation using ModelBinder

I am developing a small site using ASP.NET MVC, MySQL and NHibernate.

I have a Contact class:

[ModelBinder(typeof(CondicaoBinder))]
public class Contact {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int Age { get; set; }    
}

And binding to the model:

public class ContactBinder:IModelBinder {
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
         Contact contact = new Contact ();
         HttpRequestBase form = controllerContext.HttpContext.Request;

         contact.Id = Int16.Parse(form["Id"]);
         contact.Name = form["Name"];
         contact.Age = Int16.Parse(form["Age"]);

         return contact;
    }
}

In addition, I have a view with a form to update my database using this action:

public ActionResult Edit([ModelBinder(typeof(ContactBinder))] Contact contact) {
    contactRepo.Update(contact);

    return RedirectToAction("Index", "Contacts");
}

So far, everything is working fine here. But before updating my contact, I have to do a form check.

My question is: Where should I perform this check? In the ActionResult method or in the Model Binder? Or somewhere else?

Many thanks.

+3
source share
3 answers

Take a look at XVAL Steve Sanderson.

Your business objects must apply your business logic.

Kindness

Dan

XVal

+2

, .

, , , , . , , buisness. RuleViolations, / . bool .

: Nerd Dinner Chapter

0

, Microsoft

0

Source: https://habr.com/ru/post/1715632/


All Articles