MVC 4:在下拉列表中调用操作方法选择更改

Dav*_*het 2 c# asp.net-mvc-4

我有一个下拉列表,我希望在更改选择时调用特定的操作方法.这是我的下拉列表:

@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"})
Run Code Online (Sandbox Code Playgroud)

这是动作方法:

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

出于某种原因,这种方法永远不会被击中,但是这个方法却被命中:

    public ActionResult Index()
    {
        ...
        return View(ediFileModel);
    }
Run Code Online (Sandbox Code Playgroud)

能否请你帮忙?

Sel*_*enç 5

方法的第一个参数BeginFormAction名称,第二个参数是Controller.您正在传递,null因此它使用默认值.

更改您的代码并传递Action 您要调用的名称:

@using (Html.BeginForm("FolderChange", "ControllerName", FormMethod.Post, new { id = "ediFilesForm" }))
Run Code Online (Sandbox Code Playgroud)