确定在MVC2中单击了哪个按钮 [英] Identify which button has been clicked in MVC2

查看:48
本文介绍了确定在MVC2中单击了哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图页面中有2个按钮。一个按钮是一个普通按钮,它使用javascript代码在表中动态添加行,另一个按钮是提交按钮。我希望当用户点击添加表中行的按钮时发生后期操作,即我想要知道用户何时添加一行。

解决方案

确定点击了哪个按钮将是基本滥用任何面向事件的技术,MVC与否,ASP.NET或其他任何东西。 这不是它的工作原理。您可以创建/添加按钮并分别为每个按钮提供事件句柄。这样,您不需要知道单击了哪个按钮:事件处理程序已经执行了该按钮应该用于的内容。



仅限于罕见例如,当你有很多按钮与一些变化几乎相同时,可能需要识别按钮,传递一些特定于特定按钮的数据,通常是控件数组中的索引。有时,这种技术很有用,但......主要用于编辑框,复选框和其他类似的控件。对于按钮......不,它宁可表示非常糟糕的UI设计。我不相信你需要那个;甚至不想讨论这些事情。更好地使用我在第一段中描述的方法。



-SA


添加属性在下面的提交按钮中

 <   input     type   = 提交    id   =  btnSubmit    name   = 按钮    class   = 按钮    value   = 提交    /  >  





和Access动作结果中的按钮名称如下所示

 [HttpPost] 
public ActionResult方法( string 按钮)
{
if (button == yoursubmitbutton
{
}
else
{
}

}





希望这个帮助


谢谢Jameel。我确实使用你提供的代码点击了按钮,但有一个问题,在按钮的输入标签中给出name属性我的帖子动作没有适用于任何控件(下拉列表,文本框等)。是否有解决该问题的方法?

I have 2 buttons in my view page. One button is a normal button that uses javascript code to add rows dynamically in a table and the other is a submit button.I want a post action to happen when the user clicks on the button that adds the rows in table,ie, I want to know when a row is being added by the user.

解决方案

"Identifying which button has been clicked" would be a fundamental abuse of any event-oriented technology, MVC or not, ASP.NET or anything else. This is not how it works. You create/add a button and provide an event handle to each button individually. This way, you don''t need to know which button was clicked: the event handler already does what that button was supposed to be used for.

Only in rare cases, when you have a lot of buttons doing nearly the same with some variation, identification of the button might be needed, to pass some data specific to a particular button, usually, something like an index in the array of controls. Sometimes, such technique is useful, but… mostly for edit boxes, check boxes and other similar controls. For buttons… no, it would rather indicate really bad UI design. I don''t believe you needs that; don''t even want to discuss such things. Better use the approach I described in the first paragraph.

—SA


Add an attribute in the submit button like below

<input type="submit" id="btnSubmit" name="button" class="button" value="Submit" />



and Access the button name from the action result like below

[HttpPost]
public ActionResult Method(string button)
{
if(button=="yoursubmitbutton")
{
}
else
{
}

}



Hope this helps


Thanks Jameel.I did get the button clicked using the code provided by you but have one problem,on giving the name attribute in the input tag for the button my post action doesn''t work for any control(dropdownlist,textbox,etc).Is there any solution to that problem?


这篇关于确定在MVC2中单击了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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