【发布时间】: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
}
}
代码执行时:
单击第一个提交按钮时,它会忽略 SetDefaultRole 函数并执行 RoleEdit 函数。
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