I have a form with two submit buttons in my asp.net mvc (C#) application. When i click any submit button in Google Chrome
, by default the value of submit button is the first submit button's value.
我的asp.net mvc(C#)应用程序中有一个带有两个提交按钮的表单。当我点击谷歌浏览器中的任何提交按钮时,默认情况下,提交按钮的值是第一个提交按钮的值。
Here is the html:
这是html:
<input type="submit" value="Send" name="SendEmail" />
<input type="submit" value="Save As Draft" name="SendEmail" />
<input type="button" value="Cancel" />
When i click the Save As Draft
button, in the action of the controller, it gets "Send" as the value for SendEmail
.
当我单击“另存为草稿”按钮时,在控制器的操作中,它将获得“发送”作为SendEmail的值。
Here is the action:
这是行动:
public ActionResult SendEmail(string SendEmail, FormCollection form)
{
if(SendEmail == "Send")
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
When i get the value from FormCollection, it shows "Send". i.e. form["SendEmail"]
gives Send
当我从FormCollection获取值时,它显示“发送”。即form [“SendEmail”]给出发送
What may be the problem or work around i need to do to get the actual value of the clicked submit button?
我需要做什么才能获得点击提交按钮的实际值?
4 个解决方案
#1
7
Show this page.
显示此页面。
ASP.NET MVC – Multiple buttons in the same form - David Findley's Blog
ASP.NET MVC - 同一形式的多个按钮--David Findley的博客
Create ActionMethodSelectorAttribute inherit class.
创建ActionMethodSelectorAttribute继承类。
#2
5
Try this instead:
试试这个:
<input type="submit" value="Send" name="send" />
<input type="submit" value="Save As Draft" name="save" />
and:
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
}
}
#3
1
Hidden Html elements will be submitted with your form, so you could add a hidden element and modify it on button click before submission. Return true to continue with form submission.
隐藏的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;" />
Now you can just pull the value out of your form collection.
现在,您可以从表单集中提取值。
public ActionResult SendEmail(FormCollection form)
{
if(Boolean.Parse(form["sendemail"]))
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
Rather than using the FormCollection directly though, the best practice would be to create a view model that contains the specified property.
但不是直接使用FormCollection,最佳做法是创建包含指定属性的视图模型。
View Model
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;" />
Controller Action
public ActionResult SendEmail(FooViewModel model)
{
if(model.SendEmail)
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
#4
-2
work around: use javascript for submiting the form instead of submit buttons
解决方法:使用javascript提交表单而不是提交按钮
#1
7
Show this page.
显示此页面。
ASP.NET MVC – Multiple buttons in the same form - David Findley's Blog
ASP.NET MVC - 同一形式的多个按钮--David Findley的博客
Create ActionMethodSelectorAttribute inherit class.
创建ActionMethodSelectorAttribute继承类。
#2
5
Try this instead:
试试这个:
<input type="submit" value="Send" name="send" />
<input type="submit" value="Save As Draft" name="save" />
and:
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
}
}
#3
1
Hidden Html elements will be submitted with your form, so you could add a hidden element and modify it on button click before submission. Return true to continue with form submission.
隐藏的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;" />
Now you can just pull the value out of your form collection.
现在,您可以从表单集中提取值。
public ActionResult SendEmail(FormCollection form)
{
if(Boolean.Parse(form["sendemail"]))
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
Rather than using the FormCollection directly though, the best practice would be to create a view model that contains the specified property.
但不是直接使用FormCollection,最佳做法是创建包含指定属性的视图模型。
View Model
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;" />
Controller Action
public ActionResult SendEmail(FooViewModel model)
{
if(model.SendEmail)
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
#4
-2
work around: use javascript for submiting the form instead of submit buttons
解决方法:使用javascript提交表单而不是提交按钮