C# 如何在MVC 4中触发按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16440365/
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
How to trigger button click in MVC 4
提问by
I'm new to MVC and I'm creating a Registration form for my app but my button click is not working current code is not given below
我是 MVC 的新手,我正在为我的应用程序创建一个注册表单,但我的按钮单击不起作用 下面没有给出当前代码
view
看法
<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>
@Html.Label("User Name")
</td>
<td>
@Html.TextBoxFor(account => account.Username)
</td>
</tr>
<tr>
<td>
@Html.Label("Email")
</td>
<td>
@Html.TextBoxFor(account => account.Email)
</td>
</tr>
<tr>
<td>
@Html.Label("Password")
</td>
<td>
@Html.TextBoxFor(account => account.Password)
</td>
</tr>
<tr>
<td>
@Html.Label("Confirm Password")
</td>
<td>
@Html.Password("txtPassword")
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSubmit" value="Sign Up" />
</td>
</tr>
</table>
</fieldset>
model
模型
public class Account
{
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
controller(not fully completed)
控制器(未完全完成)
public class AccountController : Controller
{
//
// GET: /Account/
public ActionResult Index()
{
return View();
}
// GET: /Account/SignUp
public ActionResult SignUp()
{
return View();
}
[HttpPost]
public ActionResult SignUp(string userName,string email,string password)
{
Account createAccount = new Account();
createAccount.Username = userName;
createAccount.Email = email;
createAccount.Password = password;
return View("Index");
}
}
how to define click event here I tried the http post but its not working I know my code is not correct please point what is the error here
如何在此处定义点击事件 我尝试了 http 帖子但它不起作用 我知道我的代码不正确 请指出这里的错误是什么
采纳答案by anaximander
ASP.NET MVC doesn't work on events like ASP classic; there's no "button click event". Your controller methods correspond to requests sent to the server.
ASP.NET MVC 不适用于 ASP 经典之类的事件;没有“按钮点击事件”。您的控制器方法对应于发送到服务器的请求。
Instead, you need to wrap that form in code something like this:
相反,您需要将该表单包装在如下代码中:
@using (Html.BeginForm("SignUp", "Account", FormMethod.Post))
{
<!-- form goes here -->
<input type="submit" value="Sign Up" />
}
This will set up a form, and then your submit input will trigger a POST, which will hit your SignUp()method, assuming your routes are properly set up (the defaults should work).
这将设置一个表单,然后您的提交输入将触发一个 POST SignUp(),假设您的路由设置正确(默认值应该工作),它将命中您的方法。
回答by CodeCaster
MVC doesn't do events. Just put a form and submit button on the page and the method decorated with the HttpPost attribute will process that request.
MVC 不做事件。只需在页面上放置一个表单和提交按钮,用 HttpPost 属性装饰的方法就会处理该请求。
You might want to read a tutorial or two on how to create views, forms and controllers.
您可能想阅读一两个关于如何创建视图、表单和控制器的教程。
回答by nercan
yo can try this code
你可以试试这个代码
@using (Html.BeginForm("SignUp", "Account", FormMethod.Post)){<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>
@Html.Label("User Name")
</td>
<td>
@Html.TextBoxFor(account => account.Username)
</td>
</tr>
<tr>
<td>
@Html.Label("Email")
</td>
<td>
@Html.TextBoxFor(account => account.Email)
</td>
</tr>
<tr>
<td>
@Html.Label("Password")
</td>
<td>
@Html.TextBoxFor(account => account.Password)
</td>
</tr>
<tr>
<td>
@Html.Label("Confirm Password")
</td>
<td>
@Html.Password("txtPassword")
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSubmit" value="Sign Up" />
</td>
</tr>
</table>
</fieldset>}
回答by Nicholas King
as per @anaximander s answer but your signup action should look more like
根据@anaximander 的回答,但您的注册操作应该看起来更像
[HttpPost]
public ActionResult SignUp(Account account)
{
if(ModelState.IsValid){
//do something with account
return RedirectToAction("Index");
}
return View("SignUp");
}

