简体   繁体   中英

ASP.NET: How can I pass the value of a variable from a view to another one

I need to pass a variable from a view to another one

so in view1 I wrote

@{
    var a1 = "Sample"
 }

in View2 I need to use the value of a1 , so I have to write in View2

@{
    var a2 = a1 (a1 value is coming from View1)
 }

Using tempdata in your Controller you can achieve this..

[HttpPost]
 public ActionResult TempEmp(string name)
{

                TempData["EmpName"] = name;
                return RedirectToAction("PermanentEmp");
}

 //Controller Action 2(PermanentEmployee)
 public ActionResult PermanentEmp()
{
               string empName = TempData["EmpName"] as string;
               return View(empName);
 }

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