tags:

views:

54

answers:

3

Which of these two scenario's is best practice in ASP.NET MVC?

1 Post to self

In the view you use

using (Html.BeginForm) {
    ...
}

And in the controller you have

    [HttpGet]
    public ActionResult Edit(int id)

    [HttpPost]
    public ActionResult Edit(EditModel model)

2 Post from Edit to Save

In the view you use

using (Html.BeginForm("Save", "ControllerName")) {

And in the controller you have

    [HttpGet]
    public ActionResult Edit(int id)

    [HttpPost]
    public ActionResult Save(EditModel model)

Summary

I can see the benefits of each of these, the former gives you a more restful style, with the same address being used in conjunction with the correct HTTP verb (GET, POST, PUT, DELETE and so on). The latter has a URL schema that makes each address very specific.

Which is the correct way to do this?

+1  A: 

Post to the same action.

Otherwise if validation fails in Save you would need to redirect to Edit. You would have to store errormessages in Tempdata and repopulate ModelState from it.

Malcolm Frexner
Thanks - I hadn't thought of this aspect of the question.
Sohnee
+2  A: 

For a RESTful controller:

// return an HTML form for editing a specific entity
public ActionResult Edit(int id) { }

// find and update a specific entity
[HttpPut]
public ActionResult Update(EditModel userView) { }

And in the View:

<% using (Html.BeginForm<HomeController>(c => c.Update(null))) {%>
    <%: Html.HttpMethodOverride(HttpVerbs.Put) %>
    <%: Html.EditorForModel() %>
    <input type="submit" value="Save" />
<% } %>
Darin Dimitrov
I don't want to start the side discussion on this, but I wanted to point out that there is a slight issue with W3 specs that mean I won't be using PUT any-time soon: http://www.w3.org/TR/html401/interact/forms.html#adef-method - but your answer is still correct in terms of REST.
Sohnee
That's why you use the `Html.HttpMethodOverride` helper. Your HTML form maybe not, but other consumers yes.
Darin Dimitrov
A: 

I prefer to use Edit/Update as I think it makes it clearer on what the action is doing. Stephen Walther has some good suggestions for standard action names using that convention.

I don't think it really maters but I would say being consistent throughout all your controllers is more important.

As a side note, if REST is a requirement of yours then I believe the following is more true to a RESTfull app:

[HttpGet]
public ActionResult Customer(int id) {
  //return customer details
}

[HttpPut]
public ActionResult Customer(Customer cust) {
  //update customer
}

[HttpPost]
public ActionResult Customer(Customer cust) {
  //insert new customer
}

If REST ins't required then I'd go with Edit/Update convention

David G
I'm not sure what side of the fence you are falling here as your opening paragraph suggests you would use different actions, but your example is the same action, with different verbs. I see that you're saying either way is fine, so maybe you've given both sides because of that?
Sohnee
I personally prefer to use Edit/Update as I believe it makes the action's intentions clearer but I'm not required to create a RESTful app. However I've given the example if REST IS a requirement of yours. I've tried to make this a bit clear in my example.
David G
Hi David - thanks!
Sohnee