Asp.net mvc 3 ActionResult HttpPost需要访问值
我正在使用MVC# 假设我有以下行动结果:Asp.net mvc 3 ActionResult HttpPost需要访问值,asp.net-mvc-3,Asp.net Mvc 3,我正在使用MVC# 假设我有以下行动结果: public ActionResult Create(string location) { ... View() } 我需要在[httppost]中使用location primary [HttpPost] public ActionResult Create(Employee employee) { ... // I need to acc
public ActionResult Create(string location)
{
...
View()
}
我需要在[httppost]中使用location primary
[HttpPost]
public ActionResult Create(Employee employee)
{
...
// I need to access the value of location here but I dont' have access to the View
}
获取位置价值的最佳方式是什么。我可以创建一个viewmodel并将该值传递给视图,然后在[HttpPost]中检索它,但我无法访问该视图,因为它受到限制。在mvc中,有许多方法可以在控制器方法之间传递数据。其中之一是使用
TempData
。
您可以在GET方法中保存位置
然后在POST方法中检索它
不过,使用viewmodel更可取。为什么不使用会话变量呢?您可以使用会话变量,但是
TempData
仅用于重定向。TempData也是一种更安全的数据传递方式,因为会话可能被用户“窃取”。我无法修改视图中的任何内容。视图已创建。我们被告知任何时候都不要修改它form@NatePet-这是家庭作业吗?听起来像是家庭作业通常需要的那种任意限制。
public ActionResult Create(string location)
{
TempData["location"] = location;
...
View()
}
[HttpPost]
public ActionResult Create(Employee employee)
{
var location = TempData["location"];
...
}