2012-06-23 17 views
2

我不太了解这是如何工作的。MVC3控制器如何检索HTTPPOST参数?

从我的实体对象传递参数工作正常。但是当我创建新的字段时,只有第一个被检索。

型号用户类别:

public class User { 

    [Key] 
    public long Uid { get; set; } 

    [Required] 
    [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email:")] 
    public string Email { get; set; } 

    [Required] 
    [StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)] 
    [Display(Name = "User Name:")] 
    public string Username { get; set; } 

    public string Password { get; set; } 

    public byte Role { get; set; } 

    public DateTime Created { get; set; } 
} 

CSHTML:

@using (Html.BeginForm(null, 
        null, 
        FormMethod.Post, 
        new { id = "regform" }) 
    ) { 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>Register</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Email) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Email) 
     @Html.ValidationMessageFor(model => model.Email) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Username) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Username) 
     @Html.ValidationMessageFor(model => model.Username) 
    </div> 

    <div class="editor-label"> 
     Password: 
    </div> 
    <div class="editor-field"> 
     @Html.Password("pwd") 
    </div> 

    <div class="editor-label"> 
     Confirm Password: 
    </div> 
    <div class="editor-field"> 
     @Html.Password("confirm") 
    </div> 

    <p> 
     <input type="submit" value="Register" /> 
    </p> 
</fieldset> 
} 

控制器:

[HttpPost] 
    public ActionResult Register(User user, string pwd, string confirm) { 
     user.Username = confirm; 
     user.Created = DateTime.Now; 
     user.Role = 255; 
     user.Password = EncryptPassword.Password(pwd); 


     if (ModelState.IsValid && pwd == confirm) { 
      db.Users.Add(user); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(user); 
    } 

当我越来越糊涂,是pwd挑选了罚款。另一方面,confirm仍然是null。我最初认为这是模型中的命令和confirm,简直就是conPwd。如果这不起作用,我将其名称更改为confirm。它仍然没有工作,我找不到解释如何将多个参数传递给控制器​​的任何内容。

编辑: 更新我的代码。不管你信不信,这一个人一天中的大部分时间都在写我,因为我一直在努力了解自己在做什么。当你在同一时间学习实体,LINQ,MVC,ASP.NET和Razor时,需要考虑很多东西。基本的C#是我进入这种认识的唯一部分。 :)

+0

为什么你不使用强类型视图? –

+0

我是,但密码栏只是一个带有自定义名称的文本框。它传递给控制器​​,然后通过模型加密,然后传回。这不是正确的做法吗?我完全不熟悉ASP.NET MVC。 – Blanky

+0

您是否尝试过使用表单集合 –

回答

1

您需要您的RegisterModel的强类型视图,然后使用Html.BeginForm将数据发布到控制器。

模型

// This is the Model that you will use to register users 
public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email address")] 
    public string Email { 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; } 
} 

视图(CSHTML)

// This is your strongly typed view that will use 
// model binding to bind the properties of RegisterModel 
// to the View. 
@model Trainer.Models.RegisterModel 

// You can find these scripts in default projects in Visual Studio, if you are 
// not using VS, then you can still find them online 
<script src="~/Scripts/jquery.validate.min.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 

// This is where your form starts 
// The "Account" parameter states what controller to post the form to 
@using (Html.BeginForm((string)ViewBag.FormAction, "Account")) { 
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") 

    <fieldset> 
     <legend>Registration Form</legend> 
     <ol> 
      <li> 
       @Html.LabelFor(m => m.UserName) 
       @Html.TextBoxFor(m => m.UserName) 
       @Html.ValidationMessageFor(m => m.UserName) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Email) 
       @Html.TextBoxFor(m => m.Email) 
       @Html.ValidationMessageFor(m => m.Email) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Password) 
       @Html.PasswordFor(m => m.Password) 
       @Html.ValidationMessageFor(m => m.Password) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.ConfirmPassword) 
       @Html.PasswordFor(m => m.ConfirmPassword) 
       @Html.ValidationMessageFor(m => m.ConfirmPassword) 
      </li> 
     </ol> 
     <!-- The value property being set to register tells the form 
      what method of the controller to post to --> 
     <input type="submit" value="Register" /> 
    </fieldset> 
} 

控制器

// The AccountController has methods that only authorized 
// users should be able to access. However, we can override 
// this with another attribute for methods that anyone 
// can access 
[Authorize] 
public class AccountController : Controller 
{ 

    // This will allow the View to be rendered 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     return ContextDependentView(); 
    } 

     // This is one of the methods that anyone can access 
     // Your Html.BeginForm will post to this method and 
     // process what you posted. 
     [AllowAnonymous] 
     [HttpPost] 
     public ActionResult Register(RegisterModel model) 
     { 
      // If all of the information in the model is valid 
      if (ModelState.IsValid) 
      { 
       // Attempt to register the user 
       MembershipCreateStatus createStatus; 
       Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus); 

       // If the out parameter createStatus gives us a successful code 
       // Log the user in 
       if (createStatus == MembershipCreateStatus.Success) 
       { 
        FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); 
        return RedirectToAction("Index", "Home"); 
       } 
       else // If the out parameter fails 
       { 
        ModelState.AddModelError("", ErrorCodeToString(createStatus)); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

} 
+0

你让我以一种完全不同的方式思考,并更多地与ASP.Net MVC一致。一个问题:我在将密码存储在数据库中之前实际上是对密码进行了加密(模型是一个实体)。我会在哪里放这个功能?我会更新我的代码。我没有意识到我离开太多了。 – Blanky

+0

您可以在您的会员供应商中添加加密。当我调用Membership.CreateUser(...)然后我加密密码并将其发送到数据库。当我调用Log on方法时,我会加密密码并将其与数据库中的加密密码进行比较。这非常简单,如果您将其写入成员身份一次,并在需要的地方重新使用。 –

+0

太棒了,谢谢。 – Blanky