tags:

views:

2785

answers:

3

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?

+5  A: 

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.

rodbv
This answer is correct. I would add that, whereas the element names can and should be the repeated for each form, each element should have a unique ID.
Tim Scott
If I am using an ASP.NET user control for each row, and in the control I am using ASP.NET text boxes and buttons, is it possible to do the whole repeated name, unique ID thing?
skb
Or you can also create a simple html button and get its value on Controller's post method:http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx
Junior Mayhé
A: 

As rodbv said you want to use seperate <form> elements.

When you are using Asp.Net MVC or classic html (php, classic asp, etc) you have to forget the Asp.Net way of handling button presses. When a form is posted back to the webserver all the server knows about is simply "the form was sent, and contained the following input elements".

Asp.net (standard) adds a wrapper round many of the standard html postback actions using javascript (the __doPostback javascript function is used almost everywhere) and this adds information about which input element of the form caused the postback and delivers it to the server in a hidden form variable. You could mimic this behavior if you really so desired, but I would recomend against it.

It may seem strange 'littering' a page with many <form>'s, however it will mean that the postback to the server will be lighter weight and should make everything run that little bit faster for the user.

Ash
A: 

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.

Interfector