【问题标题】:using two submit buttons inside single form在单个表单中使用两个提交按钮
【发布时间】:2011-01-26 06:05:17
【问题描述】:

我的 asp.net mvc (C#) 应用程序中有一个带有两个提交按钮的表单。当我点击Google Chrome 中的任何提交按钮时,默认情况下提交按钮的值是第一个提交按钮的值。

这里是html:

 <input type="submit" value="Send" name="SendEmail" />
 <input type="submit" value="Save As Draft" name="SendEmail" />
 <input type="button" value="Cancel" />

当我单击Save As Draft 按钮时,在控制器的操作中,它会“发送”作为SendEmail 的值。

下面是动作:

public ActionResult SendEmail(string SendEmail, FormCollection form)
 {
       if(SendEmail == "Send")
       {
          //Send Email
       }
       else
       {
          //Save as draft
       }
       return RedirectToAction("SendEmailSuccess");
 }

当我从 FormCollection 获取值时,它显示“发送”。即form["SendEmail"] 给出Send

我需要做些什么才能获得点击提交按钮的实际值?

【问题讨论】:

  • 您的代码看起来不错,该技术应该可以工作。可能会尝试检查 HTTP POST 以查看究竟是什么被发送回服务器。
  • 它只发生在谷歌浏览器中,但在 IE 和 Firefox 中,它运行良好。
  • Chrome 是怎么回事?!

标签: asp.net-mvc formcollection


【解决方案1】:

显示此页面。

ASP.NET MVC – Multiple buttons in the same form - David Findley's Blog

创建 ActionMethodSelectorAttribute 继承类。

【讨论】:

  • 这个链接还展示了如何在不使用 ActionMethodSelectorAttribute 的情况下处理多个按钮(这是我需要的!)谢谢!
【解决方案2】:

试试这个:

<input type="submit" value="Send" name="send" />
<input type="submit" value="Save As Draft" name="save" />

和:

public ActionResult SendEmail(string send, FormCollection form)
{
    if (!string.IsNullOrEmpty(send))
    {
        // the Send button has been clicked
    } 
    else
    {
        // the Save As Draft button has been clicked
    }
}

【讨论】:

  • 在谷歌浏览器中单击任一按钮时返回值“发送”,但在 IE 中单击“另存为草稿”时返回 null。问题仅在使用谷歌浏览器时
  • 你在仔细阅读我的帖子吗?你注意到按钮的名称和传递给动作的参数名称了吗?
  • 是的,我根据您的回答更改了我的代码,但问题是一样的。我不知道谷歌浏览器有什么奇怪的。
【解决方案3】:

隐藏的 Html 元素将与您的表单一起提交,因此您可以添加隐藏元素并在提交前单击按钮对其进行修改。返回 true 以继续提交表单。

@Html.Hidden("sendemail", true)
<input type="submit" value="Send"
       onclick="$('#sendemail').val(true); return true" />
<input type="submit" value="Save As Draft"
       onclick="$('#sendemail').val(false); return true;" />

现在您可以从表单集合中提取值。

public ActionResult SendEmail(FormCollection form)
{
   if(Boolean.Parse(form["sendemail"]))
   {
      //Send Email
   }
   else
   {
      //Save as draft
   }
   return RedirectToAction("SendEmailSuccess");
}

最好的做法是创建一个包含指定属性的视图模型,而不是直接使用 FormCollection。

查看模型

public class FooViewModel
{
  public bool SendEmail { get; set; }
  // other stuff
}

HTML

// MVC sets a hidden input element's id attribute to the property name, 
// so it's easily selectable with javascript
@Html.HiddenFor(m => m.SendEmail)

// a boolean HTML input can be modified by setting its value to
// 'true' or 'false'
<input type="submit" value="Send"
       onclick="$('#SendEmail').val(true); return true" />
<input type="submit" value="Save As Draft"
       onclick="$('#SendEmail').val(false); return true;" />

控制器动作

public ActionResult SendEmail(FooViewModel model)
{
   if(model.SendEmail)
   {
      //Send Email
   }
   else
   {
      //Save as draft
   }
   return RedirectToAction("SendEmailSuccess");
}

【讨论】:

    【解决方案4】:

    解决方法:使用 javascript 提交表单而不是提交按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多