Passing data from View to Controller

11,774

Solution 1

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).

Solution 2

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.

Solution 3

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

Share:
11,774
Pompair
Author by

Pompair

Updated on July 13, 2022

Comments

  • Pompair
    Pompair almost 2 years

    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?