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

发布于 2024-11-19 22:59:38 字数 835 浏览 3 评论 0原文

我将视图从 [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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神魇的王 2024-11-26 22:59:38

ASP.NET MVC 中基本上有 3 种技术来实现 PRG 模式

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

每种方法都有其优点和缺点。您可以选择最适合您场景的一种。

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.

看透却不说透 2024-11-26 22:59:38

如果您要将这些数据插入数据库,那么您应该将它们重定向到路由中包含此数据的控制器操作:

/Students/View/1

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

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

If you are inserting this data into a database then you should redirect them to a controller action that has this data in the route:

/Students/View/1

You can then write code in the controller to retrieve the data back from the database for display:

public ActionResult View(int id) {
    // retrieve from the database
    // create your view model
    return View(model);
}
萌逼全场 2024-11-26 22:59:38

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

希望有效。

One of the overrides of RedirectToAction() looks like that:

RedirectToAction(string actionName, object routeValues)

You can use this one as:

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

Hope that works.

左岸枫 2024-11-26 22:59:38

这是经典的 Post-Redirect-Get 模式 (PRG),看起来不错但我会添加一点代码。在 DisplayStudent 方法中检查 TempData 变量是否不为空,否则重定向到某些默认索引操作。这是为了防止用户按 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);
}

This is the classic Post-Redirect-Get pattern (PRG) and it looks fine but I would add one bit of code. In the DisplayStudent method check if your TempData variables are not null otherwise do a redirect to some default Index action. This is in case a user presses F5 to refresh the page.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文