c# - Passing list of objects to model with object property MVC 5 -
i trying pass list of javascript objects class object type parameter so:
[httppost, route("save")] public async task<jsonresult> save (int id, list<basedynamicsavemodel> models) { var widgets = await datacontext.widgets .asnotracking() .where(e => e.isactive) .tolistasync(); var messages = new list<object>(); if (models != null) { foreach (var model in models) { if (model.model != null) { var widget = widgets.firstordefault(e => e.name.equals(model.widgetname, stringcomparison.invariantcultureignorecase)); var dynamicviewmodel = (idynamicviewmodel)activator.createinstance(type.gettype("registration.models.dynamic." + model.widgetname + "viewmodel")); dynamicviewmodel.listorder = widget.listorder.hasvalue ? widget.listorder.value : 0; dynamicviewmodel.viewname = widget.name; dynamicviewmodel.title = widget.title; await dynamicviewmodel.save(api, model); messages.add(new { message = dynamicviewmodel.message, success = dynamicviewmodel.success }); } } } return json(messages); }
basedynamicsavemodel looks this:
public class basedynamicsavemodel : idynamicsavemodel { public string widgetname { get; set; } public ienumerable<dynamicmodelproperty> model { get; set; } }
and dynamicmodelproperty looks this:
public class dynamicmodelproperty { public string key { get; set; } public object value { get; set; } }
the issue value shows {object} when passing non-primitive types while i'm debugging , can't seem access of data.
i did try using dictionary here have dictionary< string, object> , object null created own class key/value pairs.
each module has viewmodel inherited baseviewmodel class looks this:
public class basedynamicviewmodel : idynamicviewmodel { public string viewname { get; set; } public string message { get; set; } public bool success { get; set; } public string title { get; set; } public int listorder { get; set; } public string viewicon { get; set; } public virtual task load(api.apiprovider api, int id) { throw new notimplementedexception(); } public virtual task save(api.apiprovider api, basedynamicsavemodel model) { throw new notimplementedexception(); } protected object copyproperties(object destination, basedynamicsavemodel source) { var destinationmodel = destination; var destinationproperties = destinationmodel.gettype(); foreach (var destinationproperty in destinationproperties.getproperties()) { if (destinationproperty.canread) { var value = source.model.where(e => e.key.tolower() == destinationproperty.name.tolower()).select(e => e.value).firstordefault(); if (value != null) { //need check if property nullable , underlying type var targettype = destinationproperty.propertytype.isnullable() ? nullable.getunderlyingtype(destinationproperty.propertytype) : destinationproperty.propertytype; var convertedvalue = convert.changetype(value, targettype); destinationproperty.setvalue(destinationmodel, convertedvalue, null); } } } return destinationmodel; } }
and in particular save method module having issues (the rest of code in method unimportant , can't show unfortunately):
public override async task save(api.apiprovider api, basedynamicsavemodel model) { //the module not named module3, has been renamed obvious reasons var savemodel = new module3savemodel(); savemodel = (module3savemodel)copyproperties(savemodel, model); }
in controller, if notice await dynamicviewmodel.save(api, model); i'm calling each of module's save methods, clarification.
i have 2 modules save fine because i'm passing single model primitive types , no nested objects. 3rd module i'm passing list of objects , fails. when object not primitive type shows {object} in debugger no data, can't use copyproperties method set savemodel because seems not have data.
question: how go getting list of objects ajax call generic class unwrap later using reflection or @ least how attempting doing so?
i @ loss. have list argument in controller needs generic because other modules using it. seems such simple problem google hasn't been (or perhaps google skills lacking).
thanks in advance :)
to interested in did resolve issue created separate controller so:
[httppost, route("save")] public async task<jsonresult> save(int id, list<list<dynamicmodelproperty>> models, string name) { }
which took list of modules arrays. in javascript determine if model passed save array , pass controller.
Comments
Post a Comment