c# - jQuery .serialize() issue -
c# - jQuery .serialize() issue -
i having unusual issue when serializing form post controller method. of fields beingness passed null (in case of strings or nullable values) or 0 (in case of numeric values). instance, simple configuration:
viewmodel:
public class homeviewmodel { public int fakeid { get; set; } public string stringdatavalue { get; set; } public int integerdatavalue { get; set; } public homeviewmodel() { } public homeviewmodel(int fakeid) { fakeid = fakeid; stringdatavalue = "this string data"; integerdatavalue = 42; } } controller:
public class homecontroller : controller { public actionresult index() { homeviewmodel viewmodel = new homeviewmodel(15); homecoming view(viewmodel); } [httppost] public jsonresult postdata(homeviewmodel model) { homecoming jsonresult { info = new { fakeid = model.fakeid, stringdata = model.stringdatavalue, integerdata = model.integerdatavalue } }; } } view:
@model webapplication1.models.homeviewmodel @{ viewbag.title = "home page"; } @using(html.beginform()) { @html.hiddenfor(m => m.fakeid) <div> false id: @html.displayfor(m => m.fakeid) </div> <div> string data: @html.textboxfor(m => m.stringdatavalue) </div> <div> integer data: @html.textboxfor(m => m.integerdatavalue) </div> <button id="btnpost">post</button> } @section scripts { <script type="text/javascript"> $(function () { $("#btnpost").on("click", function (e) { e.preventdefault(); var model = $("form").serialize(); console.log(model); $.post("postdata", json.stringify(model)) .success(function (d) { console.log(d); }) .error(function () { console.log("error"); }) }) }) </script> } if click post button output 2 console.log() lines:
console.log(model): fakeid=15&stringdatavalue=this+is+some+string+data&integerdatavalue=42 console.log(d): object {fakeid: 0, stringdata: "this string data", integerdata: 0}
as can see stringdatavalue made controller. however, if add together @html.hidden("dummy") in view above hidden field model.fakeid next output:
console.log(model): dummy=&fakeid=15&stringdatavalue=this+is+some+string+data&integerdatavalue=42 console.log(d): object {fakeid: 15, stringdata: "this string data", integerdata: 0}
that's little better, integerdatavalue still didn't create controller. however, if add together @html.hidden("dummy2") somewhere after model.integerdatavalue shown in view next output:
console.log(model): dummy=&fakeid=15&stringdatavalue=this+is+some+string+data&integerdatavalue=42&dummy2= console.log(d): object {fakeid: 15, stringdata: "this string data", integerdata: 42}
now of view model values beingness passed controller properly. don't understand why have set dummy fields in create happen. took me long time figure out why getting incomplete info in controller methods until realized happening.
is me? can else duplicate behavior? missing something?
take out json.stringify , seek that. like
<script type="text/javascript"> $(function () { $("#btnpost").click(function(e) { e.preventdefault(); var model = $("form").serialize(); console.log(model); $.post("home/postdata", model) .success(function(d) { console.log(d); }) .error(function() { console.log("error"); }); }); }); </script> c# jquery asp.net-mvc
Comments
Post a Comment