15 c# asp.net-mvc-4
我是MVC的新手,我正在为我的应用创建一个注册表单,但我的按钮点击不正常工作当前代码未在下面给出
视图
<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>
Run Code Online (Sandbox Code Playgroud)
模型
public class Account
{
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器(未完全完成)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
如何定义点击事件在这里我尝试了http帖子,但它不起作用我知道我的代码不正确请指出这里的错误是什么
ana*_*der 36
ASP.NET MVC不适用于ASP经典之类的事件; 没有"按钮点击事件".您的控制器方法对应于发送到服务器的请求.
相反,您需要在代码中包装该表单,如下所示:
@using (Html.BeginForm("SignUp", "Account", FormMethod.Post))
{
<!-- form goes here -->
<input type="submit" value="Sign Up" />
}
Run Code Online (Sandbox Code Playgroud)
这将设置一个表单,然后您的提交输入将触发POST,这将触及您的SignUp()方法,假设您的路由已正确设置(默认值应该有效).
Nic*_*ing 12
根据@anaximander的回答,你的注册动作看起来应该更像
[HttpPost]
public ActionResult SignUp(Account account)
{
if(ModelState.IsValid){
//do something with account
return RedirectToAction("Index");
}
return View("SignUp");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101748 次 |
| 最近记录: |