0

I have dropdownlist,I want to get the selected value from the view and store it in a variable in the controller in order to reuse it.

Here's my code :

Index.cshtml

@using (Html.BeginForm("Register", "Home", FormMethod.Post))
{            

    <table style="text-align: left; width: 900px; height: 160px;" border="0" cellpadding="2" cellspacing="2">
        <tbody>
             <tr>
                <td style="width: 559px;">
                   <select id="select1" name="select1">
                        <option value="1">1 BAL Active</option>
                        <option value="2">2 BAL Actives</option>
                        <option value="3">3 BAL Actives</option>
                        <option value="4">4 BAL Actives</option>
                        <option value="5">5 BAL Actives</option>
                        <option value="6">6 BAL Actives</option>
                    </select>
                </td>
            </tr>

        </tbody>
    </table>
<button type="submit" class="btn btn-default">Register </button>
}

In my Controller I've add a variable to get the selected value :

Controller.cs

var selectedOption = Request["select1"];

I have an error in the controller : enter image description here

ysfibm
  • 436
  • 2
  • 14
  • 30
  • Although this question was closed and visitors are being redirected to an appropriate answer, I think it is useful to note that the original question here stems from a confusion between the role of ApiController and the regular MVC Controller class. You seem to be using a WebAPI ApiController class as opposed to an MVC Controller class. If you use the latter (Controller), your code would be fine. I tested it. Because you are seeking to obtain a value from a visitor via an interactive form on a webpage, stick with the MVC *Controller*. ApiController is for coding a REST-ful interface. – ShieldOfSalvation Sep 14 '17 at 15:24

2 Answers2

2

You should use @Html.DropDownListFor for select1.

This will bind it to a property in your ViewModel.

For example,

@Html.DropDownListFor(m => m.select1, m.SelectListOptions)

The ViewModel will be available to your Controller methods.

In order to populate the Options for your dropdown, you will need to create a SelectList and populate it with SelectListItems.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • thanks, where should I create a SelectList and populate it with SelectListItems ? – ysfibm Sep 14 '17 at 13:15
  • @ysfibm You can create it in the Controller and set it on the ViewModel or you can create it inline. The former is better. – Philip Tenn Sep 14 '17 at 13:37
1

Philip was right. You should utilize the default data-binder to populate the values into your ViewModel. Nonetheless, a faster but not recommended way to read POST data in ApiController would be using:-

HttpContext.Current.Request.FormRequest.Form["select1"] .

More information about the usage can be found at MSDN.

Zephyr
  • 314
  • 2
  • 8
  • I've tried this : var selectedOption = HttpContext.Current.Request.FormRequest.Form["select1"]; I have an error : the name HttpContext does not exist in the current context – ysfibm Sep 14 '17 at 13:18
  • In that case you're missing the import of `using System.Web` then. – Zephyr Sep 14 '17 at 13:50