tags:

views:

45

answers:

1

I'm following this awful textbook, going through the basics of create/edit/delete records. The delete bit has a confirm button, and it is handled like so:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id, string confirmButton) {

It offers no explanation of why the button is passed as a string or how MVC interprets it.

Can anyone help clarify what's going on here? The Create code has no such string being passed.

+2  A: 

My first guess is that they want to make sure that the delete action is called by this confirm button. But it lacks context to tell what it does exactly... You can assign a generated value to the button when rendering the page for the current user. This value will be posted when you post the form using this button. Then you verify that it is the value posted is the same as the one originally posted when proceeding to delete. That would avoid evil-minded people to delete all your records programmatically...

But this book seems a bit old, since you can use the attribute HttpPost insteat of the verbose AcceptVerbAttribute version. Which "awefull" book are you tallking about? There are plenty of resource talking about CRUD operations with mvc. I liked Pro ASP.NET MVC by Apress. Steven Sanderson explains it quite well...

Stephane
+1 for the Steven Sanderson Recomendation
UpTheCreek
Yeah I really need a proper book on it, but just to get started I used the free chapter I mentioned in my comment above. As I am new to MVC, I am still getting to grips with working out how mvc magically knows which methods to associate with which links etc. and it can be confusing in a situation like the one I posted in understanding how the button is linked to the method and the correct way to link other buttons and links.
SLC
the parameter of an actionmethod are mapped from different sources, in a specific order. In that case it would come from the posted values. it maps the parameter by name. if you have a textbox with name id, the parameter "id" will contain the textbox value which had for name "id". That's the very simple version of how it works.To understand which ActionMethod from Which Controller gets called, you can find that by googling about Routing. It's hard to wrap your mind around it, but once you get it, it's not so difficult :)
Stephane
Great, Thanks! :)
SLC
The default MVC template comes(came?) with that extra parameter as well. I believe it's there because they can't have [HttpGet]Delete(id) [HttpPost]Delete(id) as the parameters/method names are the same. Just a dirty trick so it can compile and be easy to see what's going on. No examples ever use it. They should have DeleteRecord with an ActionName of Delete instead.
Jab