ASP.NET MVC - Model's metadata used from model object type instead of declared model of the View -
i have interface of model declared, class implementing it:
public interface imymodel { [range(1, 1000)] [display(name = "modelprop interface")] int myintproperty { get; set; } imysubmodel submodel { get; } } public interface imysubmodel { [range(1,1000)] [display(name = "submodelprop interface")] int myintsubproperty { get; set; } }
i have model implementation, different metadata:
public class mymodelimplementation:imymodel { [display(name = "modelprop class")] [range(1, 15)] public int myintproperty { get; set; } public imysubmodel submodel { get; set; } public mymodelimplementation() { submodel = new mysubmodelimplementation(); } } public class mysubmodelimplementation: imysubmodel { [display(name = "submodelprop class")] [range(1, 15)] public int myintsubproperty { get; set; } }
i have view, use model interface:
@model mvcapplicationmodelinterface.models.imymodel @using (html.beginform()) { <p>using lambda expression:</p> @html.displaynamefor(m=>m.myintproperty) @html.editorfor(m=>m.myintproperty) @html.validationmessagefor(m=>m.myintproperty) <br/> @html.displaynamefor(m=>m.submodel.myintsubproperty) @html.editorfor(m=>m.submodel.myintsubproperty) @html.validationmessagefor(m=>m.submodel.myintsubproperty) <br/> <p>using string expression:</p> @html.displayname("myintproperty") @html.editor("myintproperty") @html.validationmessage("myintproperty") <br/> @html.displayname("submodel.myintsubproperty") @html.editor("submodel.myintsubproperty") @html.validationmessage("submodel.myintsubproperty") <br/> @html.validationsummary(true) <br/> <input type="submit" name="btnsubmit" value="btnsubmit" /> }
and have controller proper binding , initialization of model:
[httpget] public actionresult index() { viewbag.message = "modify template jump-start asp.net mvc application."; var model = new mymodelimplementation(); model.myintproperty = 111; model.submodel.myintsubproperty = 222; return view(model); } [actionname("index"), httppost] public actionresult save([modelbinder(typeof(mymodelbinder))]imymodel model) { return view(model); } public class mymodelbinder : defaultmodelbinder { protected override object createmodel(controllercontext controllercontext, modelbindingcontext bindingcontext, type modeltype) { return new mymodelimplementation(); } }
the problem:
if in controller's action returns view not null model (return view(new mymodelimplementation(){...});), results see on screen are:
however if controller's action returns results null model (return view(null);), results are:
as can see, behavior lambda html helpers , string expression helpers different, , none of consistent @ , behavior looks bug:
- (expected) lambda helpers displayname taken imymodel
- (unexpected) string expression metadata taken mymodelimplementation class (another words, if use html.displaynamefor(m=>m.myintproperty) - shows metadata interface (model type declared in view), if use html.displayname("myintproperty") uses metadata of model.gettype()).
- (unexpected) validation rules , strings taken mymodelimplementation (model.gettype()) metadata, instead of declared model type (imymodel).
- (expected) string expression helper model's property taken interface in case model passed view null
- (unexpected) string expression helper submodel's property metadata not retrieved/respected @ all, in case model passed view null
question:
what best workaround asp.net mvc bug/feature? how force html extensions use metadata model type declared in view default? tried use metadatatypeattribute, in case 1 implements model freedom of overwritting original metadata specified in interface, don't want allow. i'm rather looking custom modelmetadataprovider implementation. metadata attributes not respected in case of metadatatype, example requiredattribute.
i not believe difference between editor()
, editorfor()
bug. think because editorfor()
has access type information of typed model, whereas editor has use reflection type, returns actual type , not interface type used.
i think seeing typed versions have more context , can @ interface information rather having call gettype()
.
validation working correctly because told model binder bind mymodelbinder binds mymodelimplementation. validation based on method posting , arguments takes.
Comments
Post a Comment