c# - Get a list from an HttpContext.request object -


using jquery have serialized form , send server in format:

object{ transactionid : "10779" itemlist : [{itemname:"ball", quantity: 5}, {itemname:"stuff", quantity:10}] } 

in custom asp.net modelbinder, this:

httprequestbase request = controllercontext.httpcontext.request;         list<item> itemlist = new list<item>();          foreach (var item in request.form.get("itemlist"))         {             itemlist.add(new transactionitemquantity             {                name = item.itemname                               quantity = item.quantity             });         }          return new transaction         {              transactionid = request.form.get("transactiontypeid"),              itemlist = itemlist         };     } 

however, foreach loop not work, ide doesn't yet know request.form.get("itemlist") returns array of objects. how make above code work?

if char-array request. need deserialize content of request.form.get("itemlist") list of items, can loop through them. this:

var list = jsonconvert.deserializeobject<list<item>>(request.form.get("itemlist")); 

you creating list of type item first, trying add objects of type transactionitemquantity in loop.

edit: added example


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -