c# - Similar property names in ViewModel not binding in controller action method -
recently updated our application mvc3 mvc4. in mvc4 have discovered having property names studio
, studioexecutive
in our viewmodel cause problems when posting. in controller method studio = null
when studioexecutive
being populated.
here example of our issue , hope there answer problem.
data classes:
public class testcontact { public list<testcontactitem> studio { get; set; } public list<testcontactitem> studioexecutive { get; set; } public testcontact() { studio = new list<testcontactitem>(); studioexecutive = new list<testcontactitem>(); } } public class testcontactitem { public int id { get; set; } public string name { get; set; } }
controller methods:
public actionresult testcontactview() { var vm = new testcontact(); vm.studio.add(new testcontactitem(){id=1, name = "studio contact id=1"}); vm.studioexecutive.add(new testcontactitem() { id = 2, name = "studio exec contact id=2" }); return view(vm); } [httppost] public actionresult savetestcontact(testcontact model) { return content("success"); }
view / javascript ajax post:
@using system.web.script.serialization @model testcontact <button type="button" onclick="savetestcontact();">click here post</button> <script type="text/javascript"> $(document).ready(function() { globaltestmodel = @html.raw(new javascriptserializer().serialize(model)); }); function savetestcontact() { // passing additional studio & studio executive parameter controller because not mapping correctly server side viewmodel without $.ajax({ type: "post", url: "/test/savetestcontact", data: json.stringify(globaltestmodel), datatype: "json", contenttype: "application/json; charset=utf-8", success: function (msg) { } }); } </script>
in example populating our object @ least 1 studio
, 1 studioexecutive
, render view. when button in view clicked, post same object controller method viewmodel not binding correctly studio
property set null.
*unfortunately i'm not able post images have screenshot of object showing studio
count 0 , studioexecutive
count 1
we did put breakpoint before post make sure serialization on javascript correct , object populated.
we have concluded has naming convention of 2 properties 1 being substring of other. 1 has encounter same problem , can point on right direction.
i know not "complete answer" question feel give additional insight issue, (i hope) lead "complete solution".
say have single item in each of studio
, studioexecutive
fields shown in example of op, , following in js
:
globaltestmodel = '@html.raw(new javascriptserializer().serialize(model))'; $.ajax({ type: "post", url: "/test/savetestcontact", data: json.stringify(globaltestmodel), datatype: "json", contenttype: "application/json; charset=utf-8", success: function (msg) { } });
the controller method receive studioexecutive
mentioned. if same , build object in js shown below, same result.
o = { studio: [{ id: 1, name: 'studio contact id=1', }], studioexecutive: [{ id: 2, name: 'studio exec contact id=2', }] }; $.ajax({ type: "post", url: "/test/savetestcontact", data: json.stringify(o), datatype: "json", contenttype: "application/json; charset=utf-8", success: function (msg) { } });
now here comes interesting part. if add item studio
field , post same way we same result.
// in controller , same ajax post public actionresult testcontactview() { var vm = new testcontact(); vm.studio.add(new testcontactitem(){id=1, name = "studio contact id=1"}); vm.studio.add(new testcontactitem(){id=3, name = "studio contact id=3"}); vm.studioexecutive.add(new testcontactitem() { id = 2, name = "studio exec contact id=2" }); return view(vm); }
but if build object in js
shown below controller receive both studio
, 2 items, , studioexecutive
, 1 item.
o = { studio: [{ id: 1, name: 'studio contact id=1', }, { id: 2, name: 'studio contact id=2', }], studioexecutive: [{ id: 2, name: 'studio exec contact id=2', }] }; // ajax post
so have learned
this tells 2 things:
- there wrong default model binder in cannot bind two, or more, fields starts same name
json.stringify
can not if there 1 item, can if there more 1 item in field has shortest name (e.g.studio
)
Comments
Post a Comment