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

本文关键字:方法 选择 调用 下拉列表 MVC | 更新日期: 2023-09-27 18:05:26

我有一个下拉列表,我想在改变选择时调用一个特定的操作方法。下面是我的下拉列表:

@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"})

下面是动作方法:

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

由于某些原因,这个方法没有被击中,但是这个方法被击中了:

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

接下来我可以尝试什么?

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

BeginForm方法的第一个参数是Action的名称,第二个是Controller,因为您传递的是null,所以它使用默认值

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

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

尝试下面的代码:-

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

问题发生是因为你没有在beginform中传递足够的信息。

查看更多信息:-

如何编写html . beginfo "在剃须刀