Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping individual buttons on ASP.NET MVC View to controller actions

Tags:

asp.net-mvc

I have an application where I need the user to be able to update or delete rows of data from the database. The rows are displayed to the user using a foreach loop in the .aspx file of my view. Each row will have two text fields (txtName, txtDesc), an update button, and a delete button. What I'm not sure of, is how do I have the update button send the message to the controller for which row to update? I can see a couple way of doing this:

  1. Put each row within it's own form tag, then when the update button is clicked, it will submit the values for that row only (there will also be a hidden field with the rowId) and the controller class would take all the post values as parameters to the Update method on the controller.
  2. Somehow, have the button be scripted in a way to send back only the values for that row with a POST to the controller.

Is there a way of doing this? One thing I am concerned about is if each row has different names for it's controls assigned by ASP.NET (txtName1, txtDesc1, txtName2, txtDesc2), then how will their values get mapped to the correct parameters of the Controller method?

like image 273
skb Avatar asked Jan 02 '09 19:01

skb


2 Answers

You can use multiple forms, and set the action on the form to be like this:

<form method="post" action="/YourController/YourAction/<%=rowId%>">

So you will have YourController/YourAction/1, YourController/YourAction/2 and so on.

There is no need to give different names to the different textboxes, just call them txtName, txtDesc etc (or even better, get rid of those txt prefixes). Since they are in different forms, they won't mix up.

Then on the action you do something like

public ActionResult YourAction(int id, string username, string description)

Where username, description are the same names that you used on the form controls (so they are mapped automatically). The id parameter will be automatically mapped to the number you put on the form action.

like image 56
rodbv Avatar answered Oct 20 '22 05:10

rodbv


You can also have multiple "valid-named" buttons on the form like:

<input type="submit" value="Save" name="btnSave" id="btnSave"/>
<input type="submit" value="Delete" name="btnDelete" id="btnDelete" /

and than check to see what submit you have received. There can be only one submit action sent per form, so it is like all the other submit buttons did not actually existed in the first place:

if ( HttpContext.Request.Form["btnDelete"] != null ) {
    //Delete stuff
} elseif ( HttpContext.Request.Form["btnSave"] != null ) {
    //Update stuff
}

I also think that you can implement a custom ActionMethodSelectorAttribute like here http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx (also listed above) to have cleaner separated code.

like image 30
Interfector Avatar answered Oct 20 '22 04:10

Interfector