asp.net mvc 3 - How to update a model that contains a list of IMyInterface in MVC3 -


i have model so:

        return new myviewmodel()         {             name = "my view model",              modules = new irequireconfig[]             {                 new fundraisingmodule()                 {                     name = "fundraising module",                     generalmessage = "thanks fundraising"                 },                  new donationmodule()                 {                     name = "donation module",                     mindonationamount = 50                 }             }         }; 

the irequireconfig interface exposes dataeditor string property view uses pass @html.editorfor so:

    @foreach (var module in model.modules)     {         <div>             @html.editorfor(i => module, @module.dataeditor, @module.dataeditor)  //the second @module.dataeditor used prefix editor fields         </div>     } 

when post controller tryupdatemodel leaves modules property null. pretty expected since wouldnt expect know concrete class deserialize to.

since have original model still available when post comes in can loop on modules , type using .gettype(). seems @ point have enough information have tryupdatemodel try deserialize model, problem uses generic type inference drive deserializer not update of properties except ones defined in interface.

how can update modules array new values?

if particular point isnt clear please let me know , try clarify

you use custom model binder. assuming have following models:

public interface irequireconfig {     string name { get; set; } }  public class fundraisingmodule : irequireconfig {     public string name { get; set; }     public string generalmessage { get; set; } }  public class donationmodule : irequireconfig {     public string name { get; set; }     public decimal mindonationamount { get; set; } }  public class myviewmodel {     public string name { get; set; }     public irequireconfig[] modules { get; set; } } 

controller:

public class homecontroller : controller {     public actionresult index()     {         var model = new myviewmodel         {             name = "my view model",             modules = new irequireconfig[]             {                 new fundraisingmodule()                 {                     name = "fundraising module",                     generalmessage = "thanks fundraising"                 },                 new donationmodule()                 {                     name = "donation module",                     mindonationamount = 50                 }             }         };         return view(model);     }      [httppost]     public actionresult index(myviewmodel model)     {         return view(model);     } } 

view:

@model myviewmodel @using (html.beginform()) {     @html.editorfor(x => x.name)     (int = 0; < model.modules.length; i++)     {         @html.hidden("modules[" + + "].type", model.modules[i].gettype())         @html.editorfor(x => x.modules[i])     }     <input type="submit" value="ok" /> } 

and custom model binder:

public class requireconfigmodelbinder : defaultmodelbinder {     protected override object createmodel(controllercontext controllercontext, modelbindingcontext bindingcontext, type modeltype)     {         var typeparam = bindingcontext.valueprovider.getvalue(bindingcontext.modelname + ".type");         if (typeparam == null)         {             throw new exception("concrete type not specified");         }         var concretetype = type.gettype(typeparam.attemptedvalue, true);         var concreteinstance = activator.createinstance(concretetype);         bindingcontext.modelmetadata = modelmetadataproviders.current.getmetadatafortype(() => concreteinstance, concretetype);         return concreteinstance;     } } 

which register in application_start:

modelbinders.binders.add(typeof(irequireconfig), new requireconfigmodelbinder()); 

now when form submitted type sent , model binder able instantiate proper implementation.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -