1

I am developing a C# MVC Application. I have an Index Action that doubles as the GET and POST Action. In the View, the user is able to search by Project Name or Number (or any other different options).

<tr>
        <th>
            Find by Name:
        </th>
        <th>
            @Html.TextBox("NameSearchString", ViewBag.CurrentNameFilter as string)
        </th>
        <th>
            <input type="submit" value="Search By Name" name="searchButton" />
        </th>
    </tr>
    <tr>
        <td></td>
    </tr>
    <tr>
        <td></td>
    </tr>
    <tr>
        <th>
            Find by Project Number:
        </th>
        <th>
            @Html.TextBox("NumberSearchString", ViewBag.CurrentNumberFilter as string)
        </th>
        <th>
            <input type="submit" value="Search By Number" name="searchByNumber" />
        </th>
    </tr>

Is it possible to inspect the value of "searchButton" in the Controller via the Request.Form?

In the Controller I currently have:

// GET: PROJECTS
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index(string sortOrder, string currentNameFilter, string currentNumberFilter, string nameSearchString, string numberSearchString, int? page, 
        string searchButton, bool? activeProjectsOnly = true)
    {

When I click on the "Search By Name" button, in the Controller the "searchButton" string ="Search By Name". That is expected, but how come I am not able to inspect it using:

this.HttpContext.Request.Form["searchButton"]

This is the MVC Level:

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc"  />
    <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
  </dependentAssembly>

Any help will be greatly appreciated.

  • possible duplicate of [How do you handle multiple submit buttons in ASP.NET MVC Framework?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – jamesSampica Apr 14 '15 at 20:10

1 Answers1

1
[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index(string sortOrder, string currentNameFilter, string currentNumberFilter, string nameSearchString, string numberSearchString, int? page, 
        string searchButton, string searchByNumber, bool? activeProjectsOnly = true)
    {

Add searchByNumber to the controller parameters.

The controller parameters look at the "name" tag when posting back as well as the Request.Form. So to get the value of the button, you have to reference it's name.

If your form has multiple buttons, I would recommend using JQuery or JavaScript to set the value of a hidden field depending on what button was clicked and pass that to your controller.

Cyberdrew
  • 1,832
  • 1
  • 19
  • 39
  • 1
    The controller isn't looking at the "name" tag. The post is sending key/value pairs. The key corresponds to the name. – jamesSampica Apr 14 '15 at 20:09
  • Thanks Cyberdrew. Your suggestion to add "searchByNumber" would in fact work in this case but I was trying to avoid having to do so. I was hoping to just be able to reference any of the Form controls/values. Is it the case that because the Action is used for GET and POST, I am unable to do so? Before I even posted the question I had already failed at using a javascript function onclick for the button so I could set the "searchButton" value in a hidden field like this: function searchByNumber() { @Html.Hidden("searchByButton", "searchByNumber") return true; } – user3258904 Apr 14 '15 at 21:02
  • Do you have a suggestion Shoe? Or are you suggesting I look elsewhere in the Form (i.e. "Keys")? – user3258904 Apr 14 '15 at 21:05
  • Sorry I wasn't more clear, yes it uses name/value pairs using the name as the key. – Cyberdrew Apr 14 '15 at 22:48
  • 1
    @Shoe - i ended up using your suggestion. Took me a while to mark your answer as accepted. Thanks much. – user3258904 Nov 06 '15 at 17:16