In ASP.NET MVC Should A Form Post To Itself Or Another Action?

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(c => c. Update(null))) {%.

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: (c => c. Update(null))) {%.

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: w3. Org/TR/html401/interact/forms. Html#adef-method - but your answer is still correct in terms of REST.

– Sohnee May 28 '10 at 9:45 That's why you use the Html. HttpMethodOverride helper. Your HTML form maybe not, but other consumers yes.

– Darin Dimitrov May 28 '10 at 9:52.

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.

Thanks - I hadn't thought of this aspect of the question. – Sohnee May 28 '10 at 9:45.

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.

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 May 28 '10 at 10:01 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 Glenn May 28 '10 at 10:28 – Sohnee May 28 '10 at 12:41.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions