Geminirand
Geminirand

Reputation: 315

MVC 4 Submit button in each row of a "grid" - how to?

How can I have a submit button in each row of a "grid", where, when that button is clicked, I can POST three pieces of data in the row to the controller?

Here's the scenario:

A screen shows my system's users in an html table "grid". One link in each row says "Associate Customer". This goes to another screen showing a list of customers that this user can be associated with. Their UserID is in the URL like AssociateCustomer/14

On the second screen, I want to display in an html grid, CustomerID, CustomerName, and a link/button that either:

  1. Says "Associate" if they aren't associated with that customer, or
  2. Says "Disassociate" if they are already associated with that customer

I got that much working, but I don't know the next part: when the user clicks a button on the grid, I need to pass CustomerID, the UserID, and "Associate/Disassociate" to the controller. How can I do that in an MVC way?

Upvotes: 3

Views: 2778

Answers (2)

jle
jle

Reputation: 9489

Something like:

@ Html.ActionLink("Click Me!","Associate","SomeController",new{userId=item.UserID,customerID=item.CustomerID})

You don't have to post the form to do what you are asking (though that would be one way to do it).

Upvotes: 3

Mike Cole
Mike Cole

Reputation: 14713

Generally you won't post the form in this case.

I would create Associate and Disassociate actions in your controller that accept the parameters you require as querystrings. Then build an ActionLink for each row of your grid that builds that appropriate URL.

Upvotes: 2

Related Questions