asp.net-mvc ASP.NET MVC 中的按钮事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4722935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button Event in ASP.NET MVC
提问by Domnic
I have created view page in MVC like
我在 MVC 中创建了视图页面,例如
<%using (Html.BeginForm())
{ %>
<%=LabelHelpers.Label("firstname", "FirstName:")%>
<br/>
<%=Html.TextBox("firstname")%>
<br/><br/>
<%=LabelHelpers.Label("lastname", "Lastname:")%>
<br/>
<%=Html.TextBox("lastname")%>
<br/><br/>
<input type="Button" value="Register"/>
<%} %>
Here I want to write Buttonclick Event ...How and Where should i write?
在这里我想写 Buttonclick 事件...我应该如何写?在哪里写?
回答by Michael Shimmins
Your input is of type button
- these don't do anything without additional client side code.
您的输入是类型的button
- 如果没有额外的客户端代码,这些就不会做任何事情。
If you want to handle the 'event' on the server in a similar way that you would have in ASP.NET, you should convert it to a submit button. Assuming your controller is called 'Account' and your action is called 'Register' your currentcode would look somethinglike this:
如果您想以与在 ASP.NET 中类似的方式处理服务器上的“事件”,则应将其转换为提交按钮。假设你的控制器被称为“帐户”和你的行动被称为“注册”您目前的代码看起来事情是这样的:
public ViewResult Register()
{
return View();
}
You want to start by passing a model to the view:
您想首先将模型传递给视图:
public ViewResult Register()
{
var registerModel = new RegisterModel();
return View(registerModel);
}
Your current view is using loosely typed inputs. Since you're passing it a model you can use strongly typed views. Your model should look something like this:
您当前的视图使用的是松散类型的输入。由于您将模型传递给它,因此您可以使用强类型视图。您的模型应如下所示:
public class RegisterMode
{
public string Firstname { get; set; }
public string Surname { get; set; }
}
To use strongly typed views, change your view to look like this:
要使用强类型视图,请将视图更改为如下所示:
<%using (Html.BeginForm())
{ %>
<%=Html.LabelFor(x => x.Firstname)%>
<br/>
<%=Html.TextBoxFor(x => x.Firstname)%>
<br/><br/>
<%=Html.LabelFor(x => x.Surname)%>
<br/>
<%=Html.TextBoxFor(x => x.Surname)%>
<br/><br/>
<input type="submit" value="Register"/>
<%} %>
What we've done is told the view to build labels and text boxes for your RegisterModel type. This will allow the model values to be automatically mapped when you POST the form to the controller.
我们所做的是告诉视图为您的 RegisterModel 类型构建标签和文本框。这将允许在您将表单发布到控制器时自动映射模型值。
Do accept the post, we need to add a new Action to the controller, with the same name, but accepting a parameter of type RegisterModel:
接受帖子,我们需要向控制器添加一个新的动作,名称相同,但接受类型为 RegisterModel 的参数:
public ActionResult Register(RegisterModel model)
{
// do something with the model, such as inserting it into the database.
// model.Firstname will contain the value of the firstname textbox
// model.Surname will contain the value of the surnaem textbox
return RedirectToAction("Success");
}
One last thing to do, to be safe, is to add the [HttpGet]
and [HttpPost]
attributes to your controller actions to control the methods they accept:
为了安全起见,最后要做的一件事是将[HttpGet]
和[HttpPost]
属性添加到您的控制器操作中以控制它们接受的方法:
[HttpGet]
public ViewResult Register()
and
和
[HttpPost]
public ActionResult Register(RegisterModel model)
I suggest you read up on MVC at http://www.asp.net/mvcand read the NerdDinner tutorial chapter in Professional MVC (available for free online in PDF format).
我建议您在http://www.asp.net/mvc 上阅读MVC并阅读 Professional MVC 中的 NerdDinner 教程章节(可免费在线获取 PDF 格式)。
回答by Arthur
joining to the question, want to make it more concrete
加入这个问题,想让它更具体
i have a form, the form already has a submit button, but i need to bind an additional action to another button. yes, i do know that MVC does not support events 'cause HTML forms doesn't support them.
我有一个表单,该表单已经有一个提交按钮,但我需要将一个附加操作绑定到另一个按钮。是的,我确实知道 MVC 不支持事件,因为 HTML 表单不支持它们。
so the solution i've came to is to create to hidden inputs inside the form and bind an 'onclick' event (jquery 'live' method) to every... oh, what the hell? here is the code:
所以我找到的解决方案是在表单内创建隐藏的输入并将“onclick”事件(jquery“live”方法)绑定到每个......哦,到底是什么?这是代码:
html:
html:
<input type="hidden" id="SenderControlID" name="SenderControlID" value="-1" />
<input type="hidden" id="SenderControlValue" name="SenderControlValue" value="-1" />
js:
js:
if ($('#SenderControlID')[0]) {
$('input[type="submit"], input[type="button"], input[type="checkbox"], input[type="radio"]').live('click', function () {
$('#SenderControlID').val($(this).attr('name'));
$('#SenderControlValue').val($(this).val());
});
}
but maybe there is more elegant solution?
但也许有更优雅的解决方案?