如何检测在asp.net中单击了哪个提交按钮(mvc) [英] How to detect which submit button was clicked in asp.net (mvc)

查看:203
本文介绍了如何检测在asp.net中单击了哪个提交按钮(mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过很多使用提交类型输入值的答案,但是我的输入按钮集合需要全部具有相同的文本。

 < input type =submitvalue =其他人使用Javascript,我也试图避免这种情况。按这个name =submitButton/> 

不起作用,因为它们都需要命名为'Press This'。

 < button type =submitvalue =12name =submitButton>按此< / button> 

不起作用,因为它没有发布该值。



有没有办法让<按钮> 提交它的值,或者改变<输入的文本type =submit> 所以他们都说在页面上有相同的值,同时有不同的值?或者可能甚至隐藏input元素的value属性中的数值,然后在使用该值之前删除Press This?

也许使用< input type =imagevalue =12/> ,并显示按此按钮图片? 编辑:尝试了< input type =image> ,它不起作用。它会提交表单,但不会使用name属性转到控制器上的正确操作。



Edit2:我还应该添加数字的提交按钮是动态的,因此我不能给他们所有不同的名称,然后看看控制器中的哪个参数传递给它的值。除非有一些方法可以处理未知数量的按钮......

解决方案

您的按钮应该如下所示:

 < button name =buttonvalue =12>按下这个< / button> 
< button name =buttonvalue =13>按下< /按钮>

然后让他们进入行动

 public ActionResult MyAction(string button)
{
if(button ==12){
//这样做
}

if(button ==13){
//做那
}
}


I've read plenty of answers that use the value of submit type input, but my collection of input buttons need to all have the same text. Others use Javascript, and I'm trying to avoid that as well.

<input type="submit" value="Press This" name="submitButton" />

Doesn't work because they all need to be named 'Press This'.

<button type="submit" value="12" name="submitButton">Press This</button>

Doesn't work because it doesn't post the value.

Is there some way to make the <button> submit it's value or to change the text of the <input type="submit"> so they all say the same on the page while having different values? Or possibly even hiding the numeric value in the value attribute of the input element and then just removing the "Press This" before using the value?

Perhaps using <input type="image" value="12" /> with a image that says "Press This"?

Edit: Tried the <input type="image"> and it doesn't work. It'll submit the form, but it doesn't use the name attribute to go to the correct action on the controller.

Edit2: I should also add, the number of submit buttons is dynamic and thus I cannot give them all different names and then see which parameter in the controller had a value passed to it. Unless there is some way to do this for a unknown number of buttons...

解决方案

your buttons should look like this:

<button name="button" value="12">Press This</button>
<button name="button" value="13">Press That</button>

then just get them in the action

public ActionResult MyAction(string button)
{
    if (button == "12"){
        //Do this
    }

    if (button == "13"){
        //Do that
    }
}

这篇关于如何检测在asp.net中单击了哪个提交按钮(mvc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆