c# - Associate PartialView to model of the page -
i have situation can't solve alone... have object:
public class service { ... public configuration conf{get; set;} ... } public class configuration { ... public list<gateway> gateways{get; set;} ... }
now have page create new service , want add runtime (client-side) partial view. have page accept model service class.. , partial view have gateway model.. seems work..
@model objectmodel.entities.configurations.service ... @section scripts { <script type="text/javascript"> function loadpartial(event) { event.preventdefault(); event.stoppropagation(); var $div = $(event.target).closest(event.data.divcontainer), url = $(this).data('url'), model = event.data.model; $.post(url, function (model) { $div.prepend(model); }); } $('#link_add_gateway').live('click', { divcontainer: "#new_gateway", model: @html.raw(json.encode(model)) }, loadpartial); </script> } ... <div id="new_gateway"> <a id="link_add_gateway" class="cursor_pointer" data-url='@url.action("rendergateway", "configuration")'>aggiungi gateway</a> </div> <input type="submit" value="create" class="btn btn-default" />
and here controller:
//edit: service valorized here too.. public actionresult rendergateway(service service) { gateway g = new gateway(); service.configuration.gateways.add(g); return partialview("~/views/_partials/gateway/edit.cshtml", g); } [httppost] public actionresult create(service service) { //still nothing }
here problem: service has no gateway valorized.. think correct, don't know how solve it! associate model of partial view model of page. how can do?
thank you
update:
public class configuration { [xmlelement(elementname = "gateway")] public gatewayslist gateways { get; set; } public configuration() { this.gateways = new gatewayslist(); } } [serializable] public class gatewayslist : list<gateway> { public gateway this[int gatewayid] { { return this.find(g => g.gatewayid == gatewayid); } } }
i think shouldn't use call because have send parameters
so try this
$().ajax({method: 'post', data: @html.raw(json.encode(model)), //other parameters})
and change
public actionresult rendergateway(objectmodel.entities.configurations.service service) { return partialview("~/views/_partials/gateway/edit.cshtml",service); }
the key problem use @html.raw(json.encode(model))
resend model on pages
update
this code came working project i'm sure works, try sending parameter string , deserialize it
public actionresult rendergateway(string service) { var jssettings = new jsonserializersettings(); jssettings.referenceloophandling = referenceloophandling.ignore; var deserializedmodel = jsonconvert.deserializeobject<service >(service, jssettings); //now deserializedmodel of type service return partialview("~/views/shared/something.cshtml", deserializedmodel); }
update 2
i see gatewaylist class it's indexer. can't serialized xmlserializer can this
public class gatewayslist : list<gateway> { [xmlignore] public gateway this[int gatewayid] { { return this.find(g => g.gatewayid == gatewayid); } } [xmlarray(elementname="gatewayslist")] [xmlarrayitem(elementname="gateway", type=typeof(gateway))] public list<gateway> gatewayslist { { } set { } } }
Comments
Post a Comment