MVC throughput between parent and child windows

Thanks in advance. Please excuse me for my grammar player. I struggled to explain my problem. In my search for a solution below the question, I started developing POC.

C # MVC No representation of skip object between views

I am having a problem using the TempData object and passing my model between my parent's popup and the popup. My problem is that I am doing TempData ["StudentViewModel"] 2 times. Pasting the first time and reading the first time are good, but read the second time, although I am sure that I am pasting the second time before the reading does not work.

I will try my best to explain this.

I have an ASP.NET page called Class.cshtml. It will have a grid of all classes. The user selects the ClassName column and opens Students.cshtml as a new grid popup with StudentName and Address columns. The user selects StudentName and opens another StudentDetails.cshtml popup window.

We have ClassController.cs which is used by all popups and has C # methods. ClassController.js has all javscript codes.

public ActionResult GetStudentsDetails() { // this will create students for each class. //Inside student for each class it will also create Student Details. // First Insert TempData["StudentViewModel"] = studentViewModel; return View("Students", studentViewModel); } 

Students.cshtml is an existing popup as shown below

 <div> //this is in for 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"] as StudentViewModel) </span> </div> 

As soon as the user selects the StudentName popup window in Students.cshtml, the js method is called, which opens a popup window with a child window that has specific student information.

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 is our custom method to call controller action method var retVal = __callback("OnNameSelected", inputParam); var postedData = JSON.parse(retVal.return_value); if (postedData.Success == true) { // i need to do like below since Model to my popup is dynamic multipleMatchPopup = window.open('', '', properties); multipleMatchPopup.document.write(postedData.PartialViewHtml); } } } 

ClassController.cs

 public JsonResult OnNameSelected(StudentViewModel StudentVM, int selectedIndex, string name, string anchorElementId) { // this will create student name details viewmodel for selected name and modify StudentViewModel object. // for example StudentDetailsViewModel vm = StudentVM[selectedIndex].DetailsVM; //since user made selection update few properties in vm StudentVM[selectedIndex].DetailsVM = vm; //Second insert // make sure to 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 the StudentDetails.cshtml popup I like this

 <div> ..... <input class="ButtonLikeHyperLink" id="@buttonId" onclick="OnUserSelectStudentDetails()" value="[Select]" type="button" /> //Second read //in fiddler innertext is show as null <span id="lblHDNStudentDetailsViewModel"> @Newtonsoft.Json.JsonConvert.SerializeObject(TempData["StudentViewModel"] as StudentViewModel) </span> </div> 

ClassController.js

 function OnUserSelectStudentDetails() { var inputParam = {}; var hiddenField = document.getElementById("lblHDNStudentDetailsViewModel"); if (hiddenField != null) { //hiddenField.innerText is null inputParam.StudentVM = JSON.parse(hiddenField.innerText); var retVal = __FAFdoCallback("OnUserSelectLenderMatchingFee", inputParam); ... } } 

ClassController.cs

 public JsonResult OnUserSelectLenderMatchingFee(StudentViewModel StudentVM) { //StudentVM is null here } 

UPDATE

Decision

I feel really stupid about this. As a great detective, Hercule Poirot said: β€œBig gray cells don't work,” mine didn't work either. Sometimes we think so far from the box that we observe the basics. I thought this thing could not be done so simply, so I thought about TempData and so on and forgot the fundamental thought that my parent pop-up already has a hidden field and I can read it and write it to it in mine javascript methods of parent and child popups and pass them to controller action methods and return the updated and consistent model back.

Taking this basic decision, I made

Students.cshtml

 <span id="lblHDNStudentViewModel"> @Newtonsoft.Json.JsonConvert.SerializeObject(Model) </span> 

Read this in the parent javascript window as shown below in ClassController.js

 function ShowStudentDetails(selectedIndex, name, anchorElementId) { var inputParam = {}; //read var hiddenField = document.getElementById("lblHDNStudentViewModel"); } 

Read this from the javascript method of the child window, as shown below in ClassController.js

 function OnUserSelectStudentDetails() { var inputParam = {}; // read in child window and access parent window element var hiddenField = window.opener.document.getElementById("lblHDNStudentViewModel"); } 

Write back to window parent from parent javascript window as below

 document.getElementById("lblHdnCDLenderViewModel").innerText = JSON.stringify(postedData.data); 

Write back to window parent from javascript child window as below

 window.opener.document.getElementById("lblHdnCDLenderViewModel").innerText = JSON.stringify(postedData.data); 
0
javascript jquery asp.net-mvc
Apr 19 '16 at 21:12
source share

No one has answered this question yet.

See similar questions:

2
C # MVC No representation of skip object between views

or similar:

334
Call child method from parent
269
Difference between ApiController and Controller in ASP.NET MVC
252
Pass the details to the parent component in the React.js file
145
jQuery stop child event trigger parent event
141
Difference between jQuery parent (), parent () and closeest () functions
3
how to stop reloading the parent window when opening the child window
2
C # MVC No representation of skip object between views
0
Setting parent attribute value from child window using javascript
0
Close popup when sending and displaying results in parent window
-one
How can I get the value of the Javascript selection parameter that will be sent to the database by mail



All Articles