使用两个按钮MVC调用两个不同的方法 [英] Invoke two different methods with two buttons MVC
本文介绍了使用两个按钮MVC调用两个不同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面中有两个按钮,我想在每个不同的按钮点击上调用两种不同的方法。
@ using (Html.BeginForm(DeleteConfirmed,Thomson))
{
@ Html.AntiForgeryToken()
< < span class =code-leadattribute> div style = float:left; class = panel123 col-sm-4 >
< div < span class =code-attribute> class = form-group >
@ Html.LabelFor(a => a.UName)
@ Html.TextBoxFor(a => a.UName)
@ Html.ValidationMessageFor(a => a.UName)
< / div >
< div class = form-group >
@ Html.LabelFor( a => a.Password)
@ Html.PasswordFor(a => a.Password)
@ Html.ValidationMessageFor(a => a.Password)
< / div >
< div 阿里gn = center >
< 输入 类型 = 提交 < span class =code-attribute> value = 提交 id = btnSubmit class = btn-default / >
< / div >
< div >
< 输入 类型 = 提交 value = 删除 < span class =code-attribute> id = btnDelete class = btn-default / >
< / div >
@ Html.ValidationMessage(CustomError)
@ Html.ValidationSummary(true)
< / div >
但我无法调用另一种方法如何才能实现此目的?
解决方案
给你的输入按钮命名相同;
< input 姓名 = 提交 .... / >
然后在你的行动中添加名称属性
public 操作DeleteConfirmed(YourModelType模型,字符串提交)
{
/ / 提交的值将是提交或删除,具体取决于按下的按钮
}
I have two buttons in my page and i want to invoke two different methods on each different button click.
@using (Html.BeginForm("DeleteConfirmed", "Thomson"))
{
@Html.AntiForgeryToken()
<div style="float:left;" class="panel123 col-sm-4">
<div class="form-group">
@Html.LabelFor(a=>a.UName)
@Html.TextBoxFor(a => a.UName)
@Html.ValidationMessageFor(a => a.UName)
</div>
<div class="form-group">
@Html.LabelFor(a=>a.Password)
@Html.PasswordFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</div>
<div align="center">
<input type="submit" value="Submit" id="btnSubmit" class="btn-default" />
</div>
<div>
<input type="submit" value="Delete" id="btnDelete" class="btn-default" />
</div>
@Html.ValidationMessage("CustomError")
@Html.ValidationSummary(true)
</div>
But i am unable to invoke another method how can i do achieve this?
解决方案
Give your input buttons the same name;
<input name="submit" .... />
then add a "name" attribute to your action
public Action DeleteConfirmed(YourModelType model, string submit) { // the value of "submit" will be "Submit" or "Delete" depending on which button was pressed }
这篇关于使用两个按钮MVC调用两个不同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文