Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC2 - Custom Model Binder Examples

I am trying to find some examples of building a custom model binder for a unique binding scenario I need to handle, but all of the articles I found were for older versions of MVC which are no longer relevant in MVC2. I've been referencing the DefaultModelBinder source code to try to get a general feel for what I need to do, but it's entirely more complicated than my scenario and I'm having trouble isolating the specific logic I need to implement.

My goal is to take a collection of Checkbox/Textbox pairs and for all of the Checked pairs I would like to create a key/value pair of the Checkbox's value and the associated Textbox's value. After aggregating this data I need to do some string serialization on the collection so I can store it in a string property of the desired Model type. I already the data being sent from the form in a manageable format which will allow me to relate a given Checkbox to a specific Textbox, it's just a matter of figuring out how to get all the pieces where I need them.

Does anyone know of some up-to-date tutorials that can get me started with building a custom model binder?

like image 880
Nathan Taylor Avatar asked Feb 26 '10 18:02

Nathan Taylor


People also ask

How can we use register a custom model binder in asp net?

To register custom model binder, we need to create a binder provider. The model binder provider class implement IModelBinderProvider interface. The all built-in model binders have their own model binder providers. We can also specify the type of argument model binder produces, not the input of our model binder.

What is the purpose of implementing the BindModel () method on the IModelBinder interface?

MVC uses following types for Model Binding: IModelBinder interface - This defines methods that are required for a Model Binder, like the BindModel method. This method is responsible for binding a model to some values using ControllerContext and BindingContext.

Is there any way to keep or preserve TempData value to multiple requests?

If you set value for TempData and read in View then the data will be deleted or will be null. If you read TempData in the first request and want to keep the value for the next request then use 'Keep' Method. If you read the TempData using 'Peek' then value persists for the next request also.


1 Answers

I don't know why you think a lot has changed since MVC 1 regarding custom model binders. But If I understand what you are trying to do, it should be fairly easy.

public class CustomModelBinder : DefaultModelBinder {
    public override object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext) {

        NameValueCollection form = controllerContext.HttpContext.Request.Form;
        //get what you need from the form collection

        //creata your model
        SomeModel myModel = new SomeMode();
        myModel.Property = "value";
        //or add some model errors if you need to
        ModelStateDictionary mState = bindingContext.ModelState;
        mState.Add("Property", new ModelState { });
        mState.AddModelError("Property", "There's an error.");

        return myModel; //return your model
    }
}

And your action :

public ActionResult Contact([ModelBinder(typeof(CustomModelBinder))]SomeModel m){
    //...
}

Was that the kind of information you are looking for?

like image 152
Çağdaş Tekin Avatar answered Oct 02 '22 08:10

Çağdaş Tekin