如何触发按钮单击MVC 4 [英] How to trigger button click in MVC 4
问题描述
我是新来的MVC,我创造了我的应用程序登记表,但我按一下按钮不能正常工作电流code以下
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
查看
<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>
模式
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");
}
}
如何在此定义click事件我尝试了HTTP POST,但它不工作我知道我的code不正确,请点是什么错误这里
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
推荐答案
ASP.NET MVC并不像ASP经典活动工作;有没有按钮单击事件。控制器方法对应于发送到服务器的请求。
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.
相反,你需要用在code是这样的形式:
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" />
}
这将建立一个表单,然后您提交的输入将触发一个POST,这将打击你的注册()
方法,假设你的路由设置正确(默认值应该工作)。
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).
这篇关于如何触发按钮单击MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!