0

在我的控制器中,我有这些动作

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]行动。

4

3 回答 3

3

您可以在视图中这样做:

@using (Html.BeginForm("Register", "yourcontroller"))
    {
      <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>
    }
于 2013-05-31T13:02:24.093 回答
1

您需要在代码中添加以下行以在控制器 Post Action 上发布视图

<form class="form-signin" method="post" action="Register">

<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>

于 2013-06-01T10:04:23.173 回答
0
@using(Html.BeginForm("Register","Controller to be forwarded",FormMethod.Post))
{
    <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>
}

我认为使用 Html 助手创建表单并添加参数FormMethod.Post可以确保事情的顺利进行。

于 2014-03-28T07:49:44.733 回答