ActionResult HttpPost需要访问值

时间:2012-07-24 21:54:51

标签: asp.net-mvc-3

我正在使用MVC C# 假设我有以下ActionResult:

    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并将该值传递给View,然后在[HttpPost]中检索它,但由于它受到限制,我无法访问View。

1 个答案:

答案 0 :(得分:1)

有许多方法可以在mvc中的控制器方法之间传递数据。其中一个是使用TempData。 您可以在GET方法中保存location

public ActionResult Create(string location) 
{ 
  TempData["location"] = location;
  ...
  View()  
} 

然后在POST方法中检索它

[HttpPost]
public ActionResult Create(Employee employee) 
{ 
  var location = TempData["location"];
  ...
} 

虽然使用viewmodel会更受欢迎。

相关问题