David Shochet
David Shochet

Reputation: 5375

MVC 4: Calling action method on dropdown list select change

I have a dropdown list, and I want a specific action method to be called on changing selection. Here is my dropdown list:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "ediFilesForm" }))
{
    var directoriesSelectList = new SelectList(Model.Directories);
    @Html.DropDownListFor(m => m.SelectedDirectory, directoriesSelectList, new {@Id = "Directories", @style = "width:Auto;height=Auto;", @size = 10, onchange = "$('#ediFilesForm').submit()", name="action:FolderChange"})

And here is the action method:

    [HttpPost]
    [ActionName("FolderChange")]
    public ActionResult FolderChange(EdiFileModel ediFileModel)
    {
        //do your work here
        return View("Index", ediFileModel);
    }

For some reason, this method is never hit, but this one is hit instead:

    public ActionResult Index()
    {
        ...
        return View(ediFileModel);
    }

What can I try next?

Upvotes: 2

Views: 12938

Answers (2)

Neel
Neel

Reputation: 11721

try below code :-

@using (Html.BeginForm("FolderChange", "ControllerName", FormMethod.Post, new { id = "ediFilesForm" }))
{
////your code
}

the problem occurs because you are not passing enough information in beginform.

for more information :-

How to write "Html.BeginForm" in Razor

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101681

First parameter of the BeginForm method is Action name, second one is the Controller.You are passing null so it uses default values.

Change your code and pass name of the Action that you want to call:

@using (Html.BeginForm("FolderChange", "ControllerName", FormMethod.Post, new { id = "ediFilesForm" }))

Upvotes: 5

Related Questions