Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - MVC3: PRG Pattern with Search Filters on Action Method

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

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:

  • You do not need to use this pattern unless you have actually stored some data (I'm not)
  • You would not use this pattern to avoid the "Are you sure you want to resubmit" message from IE when refreshing the page (guilty)

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

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Why do we need to do this ?

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.

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 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");

}

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...