tags:

views:

1230

answers:

3

Hello,

In an ASP.NET MVC application, I'm making logic for Admin to accept or reject new members. I'm showing a list of members and two buttons Accept and Reject, like this:

<% foreach (var mm in (ViewData["pendingmembers"] as List<MyMember>)) %>
<% { %>
   <tr><td>Username:<%=mm.UserName %></td><td>
   <tr><td>Firstname:<%=mm.FirstName %></td><td>
   ...etc...
   <tr>
      <td>
      <% using (Html.BeginForm("AcceptPendingUser", "Admin"))
      { %>
          <input type="submit" value="Accept" />
      <% } %>
      </td>
      <td>
      <% using (Html.BeginForm("RejectPendingUser", "Admin"))
      { %>
        <input type="submit" value="Reject" />
      <% } %>
      </td>
    </tr>
<% } %>

So, the list of pending member data is in a list of MyMember-objects. Each MyMember object will be printed out member and two buttons are setup for the admin to either accept or reject a pending member.

Then, in the controller I'm separating the handling of those two input fields/forms, like this:

public ActionResult AcceptPendingUser()
{
   // TODO: Add code to save user into DB and send welcome email.
   return RedirectToAction("Index");
}

public ActionResult RejectPendingUser()
{
   // TODO: Add code to remove user from PendingUsers list and send rejection email.
   return RedirectToAction("Index");
}

I would like to directly get the object next to the button the user pressed. How can I send the MyMember object from the View to the controller? Or how do I send perhaps a numeric index with button press? Maybe with a hidden field?

Pompair

+1  A: 

The simplest option would probably be a hidden input:

<input type="hidden" value="<%=mm.Key%>" name="key" id="key" />

(name accordingly; in each form)

The two controller would then take an argument called "key" (rename to suit). If you want to parse the object from multiple inputs, you'll need a ModelBinder. Of course, rather than 2*n forms, you might consider either query-string based urls, or use something like jQuery (or some other script helper) to submit the data without needing the forms (if script is available).

Marc Gravell
OK. I'll tried that, but I can't get hold of the passed item on the controlled side? What type should the argument be? I'm trying to pass only string at this point....
Pompair
Well, string, then.
Marc Gravell
+3  A: 

Instead of using an HTML button consider using an ActionLink and construct it to include the id of the member being approved. Another alternative would be to have a checkbox (whose value is the id of the member being approved) that the admin can select for each member to be approved and a similar one for reject and one each approve/reject buttons for the entire form.

tvanfosson
+1  A: 

Answering to myself and other mvc newbies:

I got it finally working with this code:

VIEW:

 <%=Html.ActionLink(
     "Jump", 
     "Jump", 
     new { name=(ViewData["Person"] as Person).Name, 
     person=ViewData["Person"]}, 
     null) %>

CONTROLLER:

public ActionResult Index()
{
     ViewData["Title"] = "Home Page";
     ViewData["Message"] = "Welcome to ASP.NET MVC!";
     Person p = new Person();
     p.Name = "Barrack";
     p.Age = 35;
     ViewData["Person"] = p;
     return View();
}

public ActionResult Jump(string name, Person person)
{
    return View();
}

Debugging the app in the Jump method gives me nice "Barrack"-string for the name parameter, but Person parameter in null.

I also understand what the kind commenters tried to explain: it's easy to send simple data types like strings and ints to controller, but complex types such as my Person object needs something else.

Basically passing an int is enough for me. The hardest part here was figuring out the right way to set up ActionLink.

Cheers, Pom

Pompair