asp.net mvc - Pass json data from view to controller to another view

时间:2017-10-12 10:23:04

标签: c# json ajax asp.net-mvc

I request a ajax call from the View Index.cshtml to an action method in HomeController.cs and get json data that I use in Index.cshtml.

Another View Display.cshtml is called using Url.Action() from different anchor tags in Index.cshtml.Depending on the anchor tag clicked, the data will be displayed in Display.cshtml.

Now I have a requirement where the same json(used in Index.cshtml) is needed in Display.cshtml. Please let me know the best way to pass the json data with minimal calls to server & data storage on client side.

Thank you.

Ajax call in Index.cshtml

 $(document).ready(function () {
    $.getJSON("@Url.Action("GetJsonData", "Home")", function (json) { ... }

Anchor tag in Index.cshtml

<a href="@Url.Action("Display", "Home", new { Name = "Test" })">

Call Display.cshtml in HomeController.cs

public ActionResult Display(string Name)
{
   ViewBag.Name = Name;
   return View();
}

0 个答案:

没有答案