从[HttpPost]方法传递一个变量[HTTPGET]的方法 [英] Passing a variable from [HttpPost] method to [HttpGet] method

查看:151
本文介绍了从[HttpPost]方法传递一个变量[HTTPGET]的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从重定向[HttpPost]方法[HTTPGET]法的看法。我得到它的工作,但想知道这是否是做到这一点的最好办法。

下面是我的code:

  [HttpPost]
公众的ActionResult SubmitStudent()
{
StudentViewModel模型= TempData的[模型]作为StudentResponseViewModel;TempData的[ID] = model.Id;
TempData的[名称] = model.Name;返回RedirectToAction(DisplayStudent);
}[HTTPGET]
公众的ActionResult DisplayStudent()
{
计算机[身份证] = TempData的[ID];
计算机[名称] = TempData的[名称];返回查看();
}

查看:

 <%@页
语言=C#
继承=System.Web.Mvc.ViewPage
 %GT;
< HTML和GT;
 <头=服务器>
<标题> DisplayStudent< /标题>
< /头>
<身体GT;
< D​​IV>
<%=计算机[ID]%GT; < BR />
<%=计算机[名称]%GT;
< / DIV>
< /身体GT;
< / HTML>


解决方案

有3基本上在技术ASP.NET MVC实施的 PRG模式


  • 的TempData

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

  [HttpPost]
公众的ActionResult SubmitStudent(StudentResponseViewModel模型)
{
    如果(!ModelState.IsValid)
    {
        //用户填写表单时,做了一些错误=>重新显示
        返回查看(模型);
    }    // TODO:该模型是有效的= GT;做一些关于它的处理    TempData的[模型] =模型;
    返回RedirectToAction(DisplayStudent);
}[HTTPGET]
公众的ActionResult DisplayStudent()
{
    VAR模型= TempData的[模型]作为StudentResponseViewModel;
    返回查看(模型);
}


  • 查询字符串参数

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

  [HttpPost]
公众的ActionResult SubmitStudent(StudentResponseViewModel模型)
{
    如果(!ModelState.IsValid)
    {
        //用户填写表单时,做了一些错误=>重新显示
        返回查看(模型);
    }    // TODO:该模型是有效的= GT;做一些关于它的处理    //通过传递模型的属性作为查询字符串参数重定向
    返回RedirectToAction(DisplayStudent,新
    {
        ID = model.Id,
        NAME = model.Name
    });
}[HTTPGET]
公众的ActionResult DisplayStudent(StudentResponseViewModel模型)
{
    返回查看(模型);
}


  • 持久性

还有一种办法,恕我直言,最好由成坚持这种模式到了一些数据存储(如数据库或东西,然后当你想重定向到GET操作只发送一个ID,允许它来从任何地方你的模型坚持吧)。这里的模式:

  [HttpPost]
公众的ActionResult SubmitStudent(StudentResponseViewModel模型)
{
    如果(!ModelState.IsValid)
    {
        //用户填写表单时,做了一些错误=>重新显示
        返回查看(模型);
    }    // TODO:该模型是有效的= GT;做一些关于它的处理    //持久化模型
    INT ID = PersistTheModel(模型);    //通过传递模型的属性作为查询字符串参数重定向
    返回RedirectToAction(DisplayStudent,新{ID = ID});
}[HTTPGET]
公众的ActionResult DisplayStudent(INT ID)
{
    StudentResponseViewModel模型= FetchTheModelFromSomewhere(ID);
    返回查看(模型);
}

每个方法都有其优点和缺点。由你来选择哪一个最适合你的方案。

I am redirecting the view from [HttpPost] method to [HttpGet] method. I have gotten it to work, but want to know if this is the best way to do this.

Here is my code:

[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(); 
}

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>

解决方案

There are basically 3 techniques in ASP.NET MVC to implement the PRG pattern.

  • TempData

Using TempData is indeed one way of passing information for a single redirect. The drawback I see with this approach is that if the user hits F5 on the final redirected page he will no longer be able to fetch the data as it will be removed from TempData for subsequent requests:

[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); 
}

  • Query string parameters

Another approach if you don't have many data to send is to send them as query string parameters, like this:

[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); 
}

  • Persistence

Yet another approach and IMHO the best consists into persisting this model into some data store (like a database or something and then when you want to redirect to the GET action send only an id allowing for it to fetch the model from wherever you persisted it). Here's the pattern:

[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); 
}

Each method has its pros and cons. Up to you to choose which one suits best to your scenario.

这篇关于从[HttpPost]方法传递一个变量[HTTPGET]的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆