在我的控制器中,我有这些动作
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(RegisterModel user)
{
if (ModelState.IsValid )
{
var regUser = _db.Users.Create();
regUser.UserName = user.UserName;
regUser.Password = user.Password;
_db.Users.Add(regUser);
_db.SaveChanges();
}
return RedirectToAction("Index");
}
我的看法是这样的
@model BootstrappingMvc.Models.RegisterModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_BootstrapLayout.empty.cshtml";
}
<form class="form-signin">
<h2 class="form-signin-heading">Register New User</h2>
<div class ="input-block-level">@Html.TextBoxFor(model=>model.UserName, new{@placeholder = "UserName"})</div>
<div class ="input-block-level">@Html.PasswordFor(model=>model.Password, new{@placeholder ="Password"})</div>
<div class ="input-block-level">@Html.PasswordFor(model=>model.ConfirmPassword, new{@placeholder = "Confirm Password"})</div>
<button class="btn btn-large btn-primary" type="submit">Register</button>
</form>
这是我的RegisterModel
public class RegisterModel
{
public int Id { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
[httpPost]
当用户单击注册按钮时,我不应该被转发到控制器的操作吗?现在,[httpget]
当单击按钮时它需要我采取行动,我在浏览器中得到了这个http://localhost:47944/Portal/Register?UserName=Biplov+&Password=123456&ConfirmPassword=123456
我真的无法理解我是如何在浏览器中获取这些值的,以及为什么我没有被转发到[httpPost]
行动。