jquery - Partial View uses different model than page it's contained in? -
i have partial view in main page uses different model main page uses.
in nutshell have dropdown control on main page uses 1 model populate options. when option selected, have script post selected option action method in home controller. method takes value, , runs db query dependent on value. put results model , pass model partial view. partial view rendered , converted html markup , passed script in json object. script writes resulting view markup div in main page.
my problem error saying partial view looking 1 model, when model it's getting main page's model - not own. there easy way can remedy this? can't wrap both models in model (view model) because 2 models passed through different action methods.
here's relevant code assist:
controller
public class homecontroller : controller { // loaded when page first requested. displays dropdown // distinct sampling events. public actionresult index() { // set page title viewbag.title = "sample tracker tool"; // calculate range filter samples datetime ninemonthsago = datetime.now.adddays(-270); // retrieve sampling events within filter range var context = new edms_entities(); var resultset = (from samplingevents in context.edms_samples samplingevents.record_created_date >= ninemonthsago orderby samplingevents.sampling_event select samplingevents.sampling_event).distinct(); // create viewmodel sampling events dropdown use var viewmodel = new samplingeventsvm(); viewmodel.samplingevents = new selectlist(resultset); return view(viewmodel); } // when selected sampling event changed, jquery posts selected value // method. selected value used return samples // selected sampling event. public actionresult retrievesamples(string samplingevent) { // returns selected sampling event // todo: use sampling event retrieve samples , construct list of // sample objects selected sampling event. these samples used // display sample's list of analytes. // retrieve samples selected sampling event // **samples query in progress...** // todo: finish constructing full query var context = new edms_entities(); var resultset = samples in context.edms_samples join stations in context.edms_sample_stations on samples.station_seq equals stations.station_seq samples.sampling_event == samplingevent select new { samples.sample_id, stations.station_id, samples.collection_date, samples.comments }; // assign results model objects , add resulting model objects observablecollection instance "sampleresults" observablecollection<sample> sampleresults = new observablecollection<sample>(); foreach(var row in resultset) { var newsample = new sample(row.sample_id, row.station_id, row.collection_date, row.comments); sampleresults.add(newsample); } // pass sample objects partial view , parse html markup rendered partial view var partialviewhtml = renderviewtostring(this.controllercontext, "~/views/home/retrievesamples.cshtml", sampleresults); // add partial view html markup new jsonresult object jsonresult result = new jsonresult(); result.data = partialviewhtml; return result; } // convert partial view html string private string renderviewtostring(controllercontext context, string viewname, object model) { if (string.isnullorempty(viewname)) { viewname = context.routedata.getrequiredstring("action"); } var viewdata = new viewdatadictionary(model); using (var writer = new stringwriter()) { var viewresult = viewengines.engines.findpartialview(context, viewname); var viewcontext = new viewcontext(context, viewresult.view, viewdata, new tempdatadictionary(), writer); viewresult.view.render(viewcontext, writer); return writer.getstringbuilder().tostring(); } } }
main view
<!doctype html> <html> @using sampletracking.models.viewmodels; @model samplingeventsvm <head> <title>@viewbag.title</title> <script src="~/scripts/jquery-2.1.1.js"></script> <script type="text/javascript" src="~/scripts/customscripts.js"></script> </head> <body> <span id="samplingeventdiv"> @html.dropdownlistfor(model => model.selectedsamplingevent, model.samplingevents, new { @id = "samplingeventselection" }) </span> <div id="samplingeventdetails"> @html.partial("~/views/home/retrievesamples.cshtml") </div> </body> </html>
partial view
@using system.collections.objectmodel; @using sampletracking.models.viewmodels; @model observablecollection<sample> <div id="samplingeventdetails"> @foreach (var sample in model) { <div>sample.sampleid</div> } </div>
have tried...
html.partial("~/views/home/retrievesamples.cshtml",new somemodel())
Comments
Post a Comment