1

I'm trying to call the action in the controller when the dropdown selected item changes. Here is the code I am using but it is not working.

 @Html.DropDownList("UserID", null, new { @onchange = "location.href='@Url.Action("Action", "Controller")'" })

What is the correct syntax? I tried to call a javascript function and it works.

@Html.DropDownList("UserID", null, new { @onchange = "leaveChange(this);" })

leaveChange(control) is my javascript function.

However, I am unable to invoke the action of the controller. Also, How do I then pass the value of the selected item to the action?

3
  • Are you trying to navigate to that action on the change event ? Why not use a link for that instead of the SELECT element change ? Commented Jun 19, 2018 at 20:21
  • @Shyju that is what I ended up doing. The change event of jquery redirects to that action. However I am wondering if there is a more direct way of doing it. Commented Jun 19, 2018 at 20:45
  • But for navigating to another page, a link is more appropriate ? Why do you want to bring a SELECT element in that ? Commented Jun 19, 2018 at 20:47

5 Answers 5

4

You cannot call a C# method inside the htmlAttributes object where it expects a key value pair. Instead you could execute the Url.Action method and set the result(the url) on a parent element's data attribute and read from that in your javascript code

<div data-submit-url="@Url.Action("ApplyVacation","Jobs")">
    @Html.DropDownList("UserID",)
</div>

and in the change event, read it

$(document).ready(function ()
{
    $("#UserID").change(function ()
    {
        var $this = $(this);
        var url = $this.parent().data("submit-url") + "?userId=" + $this.val();
        window.location.href = url;
    });

});

When user make a selection on the SELECT, this code will navigate to the /Jobs/ApplyVacation url with querystring key userId and the selected option value as the value of that. Update the names as needed based on your code.

Sign up to request clarification or add additional context in comments.

Comments

1

Try removing @ before onchange

 @Html.DropDownList("UserID", null, new { onchange = "location.href='@Url.Action("Action", "Controller")'" })

Comments

0

Here is what I did

 @Html.DropDownList("UserID", null,new {@id = "leve" })

Jquery code as below:

<script type="text/javascript">

                $('#leve').change(function () {
                    var url = "/UserRoles/TellMeDate";
                    var name = $('#leve').val();
                    $.get(url, { parameter: name }, function (data) {
                        alert(data);
                    });


                })

            });         




        </script>

Controller:

public string TellMeDate(string parameter)
    {


            return DateTime.Today.ToString() +"-->>>"+ parameter + "!!!!";


    }

Comments

0

This code worked for me:

onchange = "location.href='" + @Url.Action("AdmissionRequirement", "Profile") + "'"

Comments

0

If you want to pass parameters to action you can use

@Html.DropDownList("UserID", null, new { onchange = "location.href='@Url.Action("Action", "Controller", 

    new {name = "Test param", category = "Test1"})'" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.