ASP.NET MVC – Best Practices for Get/Post Controller Logic

asp.net-mvcrazor

In an ASP.NET MVC project (Razor), I have a Get request, which loads two properties on a model, dependent on the property passed into the action method. So if the parameter has a value, the Group property is supplied data. But if not, the Groups collection property is supplied data.

In the post action method, when I process the data, to repopulate the view, I have to provide similar logic, and could getaway with returning Action(param) (the get response) to the caller.

My question is, based on experience, is that a good practice to get into? I see some downsides to doing that, but adds the lack of code redundancy. Or is there a better alternative?

Best Answer

Do you have to create the view from the POST? A nicer pattern might be to use Post/Redirect/Get; this way the data is POSTed to the action which simply returns a 302 redirect to the GET action with the appropriate parameters in the query string. The responsibility for rendering then remains with the GET whilst altering state on the server is kept within the 'POST'.

The additional benefit of doing this is that you solve the problem of user's making duplicate requests when refreshing the page.

In terms of Best Practice when working with the web, it might be worthwhile learning more about REST and trying to apply RESTful principles to your MVC application.