【问题标题】:MVC 5 multiple submit on one view [duplicate]MVC 5在一个视图上多次提交[重复]
【发布时间】:2016-09-09 20:40:25
【问题描述】:

我知道这已被多次询问。但到目前为止,我还没有找到可行的解决方案。

我在一个视图中有以下代码:

@using (Html.BeginForm("RoleEdit", "AccountRoles", FormMethod.Post, new { id = "updaterole" }))
{
    <button name="mySubmit" formaction="@Url.Action("SetDefaultRole", "AccountRoles")" value="MakeDefault" class="button small">Make Default</button>

    ...


    other code

    ...

    <button type="submit" class="button small" value="save" name="save" id="save">@Resources.DashBoardTags.UpdateRoleTag</button>
}

在控制器中:

   [HttpPost]
    public ActionResult SetDefaultRole()
    {

        return View();
    }

和:

    [HttpPost]
    public ActionResult RoleEdit(List<UserRole> userRole, string mySubmit)
    {

        if (mySubmit == "Save Draft")
        {
            //save draft code here
        }
        else if (mySubmit == "Publish")
        {
            //publish code here
        }
     }

代码执行时:

  1. 单击第一个提交按钮时,它会忽略 SetDefaultRole 函数并执行 RoleEdit 函数。

  2. RoleEdit 函数值 mySubmit 为 null。

请告诉我我的方式的错误!!!!

我查看了建议的解决方案:Proposed Solution

由此,我创建了一个名为 MultipleButton 的属性扩展并更改了我的代码,现在代码如下所示:

查看:

@using (Html.BeginForm("RoleEdit", "AccountRoles", FormMethod.Post, new { id = "updaterole" }))
{
   <button value="SetDefaultRole" name="action:SetDefaultRole" class="button small" formmethod="post">Make Default</button>

    ...


    other code

    ...

    <button value="RoleEdit" name="mySubmit:RoleEdit" class="button small" formmethod="post">@Resources.DashBoardTags.UpdateRoleTag</button>
}

控制器

    [HttpPost]
    [MultipleButton(Name= "action", Argument = "SetDefaultRole")]
    public ActionResult SetDefaultRole()
    {

        return View();
    }

    [HttpPost]
    [MultipleButton(Name = "action", Argument = "RoleEdit")]
    public ActionResult RoleEdit(List<UserRole> userRole)
    {
        return View();
    }

在新扩展中,执行上面建议的解决方案链接中显示的 MultipleButtonAttribute,以下代码行始终返回空值:

controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;

谁能帮忙。

【问题讨论】:

  • 为什么不创建 2 @using (Html.BeginForm for 2 button submit?
  • 让你的两个按钮具有相同的名称,并赋予它们不同的值,然后检查提交的值。
  • 我认为您的顶部按钮没有发回值信息的原因是因为类型不是type="submit"
  • 我也改变了两个按钮: 当我点击时这仍然返回 null他们中的任何一个。
  • 我查看了上面提出的解决方案并按原样编写了帮助函数,将 MultipleButton 属性应用于两个按钮。调用扩展时,传入的是正确的值,但是执行controllerContext.Controller.ValueProvider.GetValue(keyValue)这行代码时,总是返回Null。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

你可以从下面的名称标签中识别你的按钮,你需要在你的控制器中这样检查

if (Request.Form["mySubmit"] != null)
{
//Write your code here
}
else if (Request.Form["save"] != null)
{
//Write your code here
}

或者试试;

[HttpPost]
    public ActionResult RoleEdit(List<UserRole> userRole, FormCollection fc)
    {

        if (fc["mySubmit"] != null)
        {
            //save draft code here
        }
        else if (fc["mySubmit"] != null)
        {
            //publish code here
        }
     }

【讨论】:

  • 这也只是返回一个空值,没有执行任何 if 语句
  • 你的视图应该是这样的; @using (Html.BeginForm("RoleEdit", "AccountRoles", FormMethod.Post, new { id = "updaterole" })) { ...其他代码 ... }
  • 不要用你的名字标签写动作或任何东西。它应该像 name="mySubmit"
  • 将第一个按钮更改为:.....仍然返回 null
  • name="MakeDefault" 那么你在控制器中检查 if (Request.Form["MakeDefault"] != null) 吗?
猜你喜欢
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2019-08-03
相关资源
最近更新 更多