简体   繁体   中英

how to pass data from one view to another

I want to pass object data from one view to another on click of button. I am calling the action to load the second view via ajax. In the controller action I am trying to pass object data to view. But the view is not rendered. How can I achieve the scenario of passing data from one view and populating it to the controls of other view?

I want to pass object data from one view to another on click of button

When in any Action or ActionResult you have

return RedirectToAction("controllerName","viewname","parameters")

so the Action Method instead of returning a view, this will redirect to another view Action method and that view will be rendered.

You need to send the data from one view to another in form of query string or post parameters to an action.

Ex:

[HttpPost]
public ActionResult PassData(string dataFromView1)
{

 return View("View2", dataFromView1);
}

Note: Here i used a single string data in a parameter. If your View1 has more data/complex data structure, better to use ViewModel class and pass that to your intermediate controller.

I want to pass object data from one view to another on click of button

You need to pass View in HttpPost method. Look at this code.

[HttpPost]
public ActionResult YourActionName(Model model)
{
  return View("AnotherActionName", model);
}

Or you can use RedirectToAction .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM