MVC3:PRG模式与操作方法上的搜索过滤器 [英] MVC3: PRG Pattern with Search Filters on Action Method

查看:118
本文介绍了MVC3:PRG模式与操作方法上的搜索过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Index方法的控制器,该控制器具有几个可选参数,用于过滤返回到视图的结果.

I have a controller with an Index method that has several optional parameters for filtering results that are returned to the view.

public ActionResult Index(string searchString, string location, string status) {
    ...

product = repository.GetProducts(string searchString, string location, string status);

return View(product);
}

我想像下面那样实现PRG模式,但是我不确定该怎么做.

I would like to implement the PRG Pattern like below but I'm not sure how to go about it.

[HttpPost]
public ActionResult Index(ViewModel model) {
    ...
    if (ModelState.IsValid) {
        product = repository.GetProducts(model);
    return RedirectToAction(); // Not sure how to handle the redirect
    }
return View(model);
}

我的理解是,如果出现以下情况,则不应使用此模式:

My understanding is that you should not use this pattern if:

  • 除非您已经实际存储了一些数据(我没有),否则您不需要使用此模式
  • 刷新页面(有罪)时,您不会使用此模式来避免IE发出确定要重新提交"消息

我应该尝试使用这种模式吗?如果是这样,我将如何处理?

Should I be trying to use this pattern? If so, how would I go about this?

谢谢!

推荐答案

PRG 表示后重定向获取.这意味着当您将一些数据发布回服务器时,您应该重定向到GET操作.

PRG Stands for Post-Redirect-Get. that means when you post some data to the server back, you should redirect to a GET Action.

我们为什么需要这样做?

想象一下,如果您有一个Form,您可以在其中输入客户注册信息,然后单击Submit,将其发布到HttpPost操作方法.您正在从表单中读取数据并将其保存到数据库中,并且没有执行重定向.相反,您将停留在同一页面上.现在,如果刷新浏览器(只需按F5按钮),浏览器将再次执行类似的表单发布,并且HttpPost Action方法将再次执行相同的操作. IE;它将再次保存相同的表单数据.这是个问题.为避免此问题,我们使用PRG模式.

Imagine you have Form where you enter the customer registration information and clicking on submit where it posts to an HttpPost action method. You are reading the data from the Form and Saving it to a database and you are not doing the redirect. Instead you are staying on the same page. Now if you refresh your browser ( just press F5 button) The browser will again do a similar form posting and your HttpPost Action method will again do the same thing. ie; It will save the same form data again. This is a problem. To avoid this problem, We use PRG pattern.

PRG 中,单击提交",HttpPost操作方法将保存您的数据(或它要做的任何事情),然后重定向到Get请求.因此,浏览器将向该操作发送Get请求

In PRG, You click on submit and The HttpPost Action method will save your data (or whatever it has to do) and Then do a Redirect to a Get Request. So the browser will send a Get Request to that Action

RedirectToAction方法向浏览器返回HTTP 302响应,这将导致浏览器对指定的动作发出GET请求.

RedirectToAction method returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

[HttpPost]
public ActionResult SaveCustemer(string name,string age)
{
   //Save the customer here
  return RedirectToAction("CustomerList");

}

上面的代码将保存数据并重定向到客户列表"操作方法.因此,您的浏览器网址现在为http://yourdomain/yourcontroller/CustomerList.现在,如果您刷新浏览器. IT将不会保存重复的数据.它只会加载CustomerList页面.

The above code will save data and the redirect to the Customer List action method. So your browser url will be now http://yourdomain/yourcontroller/CustomerList. Now if you refresh the browser. IT will not save the duplicate data. it will simply load the CustomerList page.

在您的搜索操作方法中,您无需执行重定向到获取操作"的操作.搜索结果位于products变量中.只需将其传递到所需的视图即可显示结果.您无需担心重复的表单过帐.所以你对此很满意.

In your search Action method, You dont need to do a Redirect to a Get Action. You have the search results in the products variable. Just Pass that to the required view to show the results. You dont need to worry about duplicate form posting . So you are good with that.

[HttpPost]
public ActionResult Index(ViewModel model) {

    if (ModelState.IsValid) {
        var products = repository.GetProducts(model);
        return View(products)
    }
  return View(model);
}

这篇关于MVC3:PRG模式与操作方法上的搜索过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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