javascript - MVC pass model between Parent and Child Window -
thanks in advance. please excuse me grammer. tried best explain issue in quest of solving below question started develop poc first.
c# mvc no submit pass object between views
i having issue using tempdata object , pass model between parent popup , child popup. problem doing tempdata["studentviewmodel"] 2 times. first time insert , first time read second time read though make sure insert second time before read not working.
i try best explain clearly.
i have asp.net page called class.cshtml. have grid of class. user select classname column , opens students.cshtml new popup window has grid studentname , address columns. user select studentname , opens popup window called studentdetails.cshtml.
we have classcontroller.cs used popups , have c# methods. classcontroller.js has javscript code.
public actionresult getstudentsdetails() { // create students each class. //inside student each class create student details. // first insert tempdata["studentviewmodel"] = studentviewmodel; return view("students", studentviewmodel); }
students.cshtml existing popup window below
<div> //this in loop string anchorelementid = string.format("anchorelementid_{0}", i); string selectedindex = i.tostring(); string name = model.students[i].name; <input class="buttonlikehyperlink" id="mybtnid" onclick="showstudentdetails(@selectedindex, '@name', '@anchorelementid')" value="@model.students[i].name" type="button"/> //for loop ends here //first read <span id="lblhdnstudentviewmodel"> @newtonsoft.json.jsonconvert.serializeobject(tempdata["studentviewmodel"] studentviewmodel) </span> </div>
once user selects studentname in students.cshtml popup below js method called opens child window popup having particular student details.
classcontroller.js
function showstudentdetails(selectedindex, name, anchorelementid) { var inputparam = {}; var hiddenfield = document.getelementbyid("lblhdnstudentviewmodel"); if (hiddenfield != null) { inputparam.studentvm = json.parse(hiddenfield.innertext); inputparam.selectedindex = selectedindex; inputparam.name = name; inputparam.anchorelementid = anchorelementid; // __callback our custom method call controller action method var retval = __callback("onnameselected", inputparam); var posteddata = json.parse(retval.return_value); if (posteddata.success == true) { // need below since model popup dynamic multiplematchpopup = window.open('', '', properties); multiplematchpopup.document.write(posteddata.partialviewhtml); } } }
classcontroller.cs
public jsonresult onnameselected(studentviewmodel studentvm, int selectedindex, string name, string anchorelementid) { // create student name details viewmodel selected name , modify studentviewmodel object. // example studentdetailsviewmodel vm = studentvm[selectedindex].detailsvm; //since user made selection update few properties in vm studentvm[selectedindex].detailsvm = vm; //second insert // make sure set tempdata before renderpartialviewtostring tempdata["studentviewmodel"] = studentvm; string shtml = this.renderpartialviewtostring("~/views/_partialstudentdetailspopup.cshtml", vm); return json(new { success = true, data = studentvm, partialviewhtml = shtml, jsonrequestbehavior.allowget }); }
in studentdetails.cshtml popup have this
<div> ..... <input class="buttonlikehyperlink" id="@buttonid" onclick="onuserselectstudentdetails()" value="[select]" type="button" /> //second read //in fiddler innertext show null <span id="lblhdnstudentdetailsviewmodel"> @newtonsoft.json.jsonconvert.serializeobject(tempdata["studentviewmodel"] studentviewmodel) </span> </div>
classcontroller.js
function onuserselectstudentdetails() { var inputparam = {}; var hiddenfield = document.getelementbyid("lblhdnstudentdetailsviewmodel"); if (hiddenfield != null) { //hiddenfield.innertext null inputparam.studentvm = json.parse(hiddenfield.innertext); var retval = __fafdocallback("onuserselectlendermatchingfee", inputparam); ... } }
classcontroller.cs
public jsonresult onuserselectlendermatchingfee(studentviewmodel studentvm) { //studentvm null here }
update
solution
i feel real stupid on issue. great detective, hercule poirot said, "the great gray cells not working", mine did not work in case. think far away box oversee basics. thinking thing cannot done in simple thinking tempdata , on , forgot fundamental point parent popup have hidden field , can read , write it in javascript methods of parent , child popups windows , pass controller action methods , updated , consistent model back.
taking basic solution did
students.cshtml
<span id="lblhdnstudentviewmodel"> @newtonsoft.json.jsonconvert.serializeobject(model) </span>
read in parent window javascript method below in classcontroller.js
function showstudentdetails(selectedindex, name, anchorelementid) { var inputparam = {}; //read var hiddenfield = document.getelementbyid("lblhdnstudentviewmodel"); }
read child window javascript method below in classcontroller.js
function onuserselectstudentdetails() { var inputparam = {}; // read in child window , access parent window element var hiddenfield = window.opener.document.getelementbyid("lblhdnstudentviewmodel"); }
write parent window element parent window javascript method below
document.getelementbyid("lblhdncdlenderviewmodel").innertext = json.stringify(posteddata.data);
write parent window element child window javascript method below
window.opener.document.getelementbyid("lblhdncdlenderviewmodel").innertext = json.stringify(posteddata.data);
Comments
Post a Comment