将变量从[HttpPost]方法传递给[HttpGet]方法

时间:2011-07-13 22:04:35

标签: asp.net-mvc http-post http-get http-redirect

我将视图从[HttpPost]方法重定向到[HttpGet]方法。我已经开始工作,但想知道这是否是最好的方法。

这是我的代码:

[HttpPost] 
public ActionResult SubmitStudent()
{ 
StudentViewModel model = TempData["model"] as StudentResponseViewModel; 

TempData["id"] = model.Id; 
TempData["name"] = model.Name; 

return RedirectToAction("DisplayStudent"); 
}

[HttpGet] 
public ActionResult DisplayStudent() 
{ 
ViewData["id"] = TempData["id"]; 
ViewData["name"] = TempData["name"]; 

return View(); 
}

查看:

<%@ Page 
Language="C#"
Inherits="System.Web.Mvc.ViewPage"
 %> 
<html>
 <head runat="server"> 
<title>DisplayStudent</title> 
</head> 
<body> 
<div> 
<%= ViewData["id"]%> <br /> 
<%= ViewData["name"]%> 
</div> 
</body> 
</html>

4 个答案:

答案 0 :(得分:8)

ASP.NET MVC中基本上有3种技术可以实现PRG pattern

  • TempData的

使用TempData确实是传递单个重定向信息的一种方法。我看到这种方法的缺点是,如果用户在最终重定向页面上点击F5,他将无法再获取数据,因为它将从TempData中删除以用于后续请求:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    TempData["model"] = model;
    return RedirectToAction("DisplayStudent");
}

[HttpGet] 
public ActionResult DisplayStudent() 
{ 
    var model = TempData["model"] as StudentResponseViewModel;
    return View(model); 
}
  • 查询字符串参数

如果您没有很多要发送的数据,另一种方法是将它们作为查询字符串参数发送,如下所示:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    // redirect by passing the properties of the model as query string parameters
    return RedirectToAction("DisplayStudent", new 
    {
        Id = model.Id,
        Name = model.Name
    });
}

[HttpGet] 
public ActionResult DisplayStudent(StudentResponseViewModel model) 
{ 
    return View(model); 
}
  • 持久性

另一种方法和恕我直言最好的方法是将此模型保存到某个数据存储(如数据库或其他东西,然后当您想要重定向到GET操作时,只发送一个id,允许它从任何地方获取模型坚持下去)。这是模式:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    // persist the model
    int id = PersistTheModel(model);

    // redirect by passing the properties of the model as query string parameters
    return RedirectToAction("DisplayStudent", new { Id = id });
}

[HttpGet] 
public ActionResult DisplayStudent(int id) 
{ 
    StudentResponseViewModel model = FetchTheModelFromSomewhere(id);
    return View(model); 
}

每种方法都有其优点和缺点。由你决定哪一个最适合你的场景。

答案 1 :(得分:2)

如果要将此数据插入数据库,则应将它们重定向到路径中包含此数据的控制器操作:

/Students/View/1

然后,您可以在控制器中编写代码以从数据库中检索数据以供显示:

public ActionResult View(int id) {
    // retrieve from the database
    // create your view model
    return View(model);
}

答案 2 :(得分:0)

RedirectToAction()的一个覆盖内容如下:

RedirectToAction(string actionName, object routeValues)

您可以将此作为:

[HttpPost] 
public ActionResult SubmitStudent()
{ 
StudentViewModel model = TempData["model"] as StudentResponseViewModel; 

return RedirectToAction("DisplayStudent", new {id = model.ID, name = model.Name}); 
}

[HttpGet] 
public ActionResult DisplayStudent(string id, string name) 
{ 
ViewData["id"] = TempData["id"]; 
ViewData["name"] = TempData["name"]; 

return View(); 
}

希望有效。

答案 3 :(得分:0)

这是经典的Post-Redirect-Get模式(PRG),它看起来不错,但我会添加一些代码。在DisplayStudent方法中,检查您的TempData变量是否为null,否则重定向到某些默认的Index操作。这是用户按F5刷新页面的情况。

public ActionResult DisplayStudent() 
{ 
    if(TempData["model"] == null)
    {
        return RedirectToAction("Index");
    }

    var model = (StudentResponseViewModel)TempData["model"];
    return View(model); 
}

public ViewResult Index()
{
    IEnumerable<StudentResponseViewModel> students = GetAllStudents();
    return View(students);
}