c# - MVC controller, take parameters in as class -
i using jquery post data via ajax, , in chrome tools showing data being sent this:
permssionarray[0][permitted]:false permssionarray[0][id]:2 permssionarray[1][permitted]:true permssionarray[1][id]:3 permssionarray[2][permitted]:true permssionarray[2][id]:4
my controller this:
public actionresult updatepermissions(permset[] permssionarray)
and permset class this:
public class permset { public int id { get; set; } public bool permitted { get; set; } }
if breakpoint in controller, has 3 items in permssionarray array, values id = 0, permitted = false.
what need change fix this?
lists odd in asp.net mvc, , have options data server.
use default model binding in asp.net mvc. requires change model post format. check out phil haack's post on this. works can hairy if have lot of lists. http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
second option use jquery circumvent post , serialize form json document, post server. option 2 easier, requires handle ajax requests rather direct post / redirect actions.
- use json.stringify(model)
- return response includes next page redirect on success
- return response includes error message
i assume know how use jquery , $.ajax method.
i started leaning more towards option 2 when page's complexity starts getting larger. downside need client story. consider using knockout.js let's convert form json using ko.tojson.
let me know if me go detail on anything.
Comments
Post a Comment