Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ActionResult HttpPost需要访问值_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 ActionResult HttpPost需要访问值

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

我正在使用MVC# 假设我有以下行动结果:

    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"];
  ...
}