Transferring data from a view to a controller

In an ASP.NET MVC application, I create logic for the administrator to accept or decline new members. I show a list of participants and two Accept and Reject buttons, for example:

<% 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 item data is in the list of MyMember objects. Each MyMember object will be printed by a member, and two buttons are configured for the administrator to accept or reject the pending item.

Then in the controller, I separate the processing of these two input / form fields, for example:

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 immediately get the object next to the button pressed by the user. How can I send a MyMember object from a view to a controller? Or how can I send a numerical index with a click of a button? Maybe with a hidden field?

+3
3

:

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

(, )

"" ( ). , ModelBinder. , 2 * n URL-, , - jQuery ( script) ( script),.

+3

HTML ActionLink , . ( ), / .

+3

mvc:

, , :

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();
}

Jump "Barrack" -string name, Person null.

, : , ints, , , Person, - .

Basically, passing an int is enough for me. The hardest part here was choosing the right ActionLink.

Cheers pom

+3
source

All Articles