-1

I am getting value in a dropdown list and I wanted to get the selected value in controller when user select any value from the dropdown list. My view is -

@using (Html.BeginForm("ApReport", "Sales", FormMethod.Post))
{
    @Html.DropDownList("Ddl", null, "All", new { @class = "control-label"})

    @Html.Hidden("rddl")                                 
}

controller -

[HttpPost]
public ActionResult ApReport(ApReport Ddl)
{
  string Ddlvalue = string.Empty;
  if (Request.Form["rddl"] != null)
  {
    Ddlvalue = Request.Form["rddl"].ToString();
  }
}

but I am not getting any value. Also, I donot want to use any submit button.

Thanks in advance

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
ronibd
  • 113
  • 1
  • 2
  • 10
  • Possible duplicate of [How to get DropDownList SelectedValue in Controller in MVC4](http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4) – Ehsan Sajjad Mar 30 '16 at 09:35
  • you do not need hidden field, just change ``Request.Form["rddl"]`` to ``Request.Form["Ddl"]`` – Ehsan Sajjad Mar 30 '16 at 09:36
  • What do you mean you do not want a submit button? (how are you sending the data to the controller). And the name of you dropdown is `name="Ddl"` which will mean you model will always be `null` because the parameter in the controller is also `Ddl`. –  Mar 30 '16 at 09:37
  • @EʜsᴀɴSᴀᴊᴊᴀᴅ, i have changed what you said, but not getting any value in controller – ronibd Mar 30 '16 at 09:40
  • How are you submitting your form? – James Dev Mar 30 '16 at 09:40
  • @StephenMuecke, I want to get the value when selected from the list without using any submit button, like JQuery or any other way – ronibd Mar 30 '16 at 09:41
  • how your form is getting submitted ? is this your original code of view? – Ehsan Sajjad Mar 30 '16 at 09:41
  • @EʜsᴀɴSᴀᴊᴊᴀᴅ, This is my problem, I am not sure how I can submit my form without using any button, I like to submit as soon as any value is selected from dropdown list, but don't know how I will do this – ronibd Mar 30 '16 at 09:43
  • Then handle the dropdown's click event and call `$('form').submit()` - but that's a terrible UI and the model will be `null` anyway –  Mar 30 '16 at 09:43
  • Without submitting, your current code (server side) will not be able to know if a value has changed in your dropdown, without submitting. You might want to have a look at ajax calls, using JQuery. Let me know if you need a working example. – monstertjie_za Mar 30 '16 at 09:44
  • @StephenMuecke, how I will use click event in Razor view, I did not find anything like this – ronibd Mar 30 '16 at 09:45
  • @monstertjie_za, could you please give me an working ajax example – ronibd Mar 30 '16 at 09:46
  • `$('#Ddl').change(function() { $('form').submit(); });` –  Mar 30 '16 at 09:46
  • @StephenMuecke, when I am submitting, I am getting error message 'An unhandled exception occurred during the execution of the current web request.' – ronibd Mar 30 '16 at 09:59
  • What section of your page are you trying to update/populate with data when you select your value in the dropdown? I think we need more information before I can give you a working sample, sorry. – monstertjie_za Mar 30 '16 at 09:59
  • I have already noted none of this will work anyway. And if your having other problems, then ask a new question or edit this one with the relevant details, including the error message –  Mar 30 '16 at 10:01
  • @monstertjie_za, I wanted to get the selected value in my controller and based on the value, I want to run some calculation, but my problem, I don't know how to get selected value in controller – ronibd Mar 30 '16 at 10:02
  • I have added a possible way for you to achieve this. – monstertjie_za Mar 30 '16 at 10:37

1 Answers1

2

The use of Ajax allows you as the developer to update the main view without reloading the entire page, as well as send data to the server in the background.

This is how I would have accomplished this task.

Firstly, I would have created an action in my controller which returns a JsonResult. This will return a JSON object to your calling jquery code, that you can use to get values back into your views. Here is an example of the action method.

[HttpGet]
public JsonResult YourActionName(string selectedValue) //Assuming key in your dropdown is string
{
    var result = DoYourCalculation(selectedValue);

    return Json(new { myResult = result }, JsonRequestBehavior.AllowGet);
}

Now, you need to add your jquery code. I would recommend you place this in a seperate javascript file referenced by your view.

Here is the JQuery code, with the ajax call to the Action in your controller. The Ajax call to the server is initiated by the 'change' event of your DropDown, handled in JQuery, as can be seen below.

    $(function () {
        $(document)
            .on('change', '#Ddl', function(){
                var valueofDropDown = $(this).val();
                var url = '/YourControllerName/YourActionName';
                var dataToSend = { selectedValue: valueofDropDown }

                $.ajax({
                    url: url,
                    data: dataToSend,
                    type: 'GET',
                    success: function (dataReceived) {
                        //update control on View
                        var receivedValue = dataReceived.myResult ;
                        $('YourControlIDToUpdate').val(receivedValue);
                }
            })
        });
    };
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73