How to pass data from a view to the controller using the WebMatrix.WebSecurity dependency

丶灬走出姿态 提交于 2019-12-11 16:37:30

问题


I am coming up with null values when I try to create a new user in this registration form. I am using the WebMatrix.WebData dependencies. What I want to do is just pass the values I am entering the form and the values I am entering in the drop downs to the controller. I seem to be unable to do this because everything is coming back is null:

First in my Razor View where I am entering the data I have:

   {
       @Html.AntiForgeryToken()
       @Html.ValidationSummary(true)


       <fieldset>
           <legend>Registration Form</legend>
           <ol class="comtrexBlue-text">
               <li>
                   @Html.LabelFor(m => m.UserName)
                   @Html.TextBoxFor(m => m.UserName)
               </li>
               <li>
                   @Html.LabelFor(m => m.Password)
                   @Html.PasswordFor(m => m.Password)
               </li>
               <li>
                   @Html.LabelFor(m => m.ConfirmPassword)
                   @Html.PasswordFor(m => m.ConfirmPassword)
               </li>

               <li>
                   @Html.LabelFor(m => m.Email)
                   @Html.TextBoxFor(m => m.Email)
               </li>
           </ol>
           <button class="btn larger" type="submit">Create</button>
       </fieldset>

   } 

Here is what I have in my Controller:

````[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UserCreateNew(string dummy)
        {

          var m = new UserCreateNewViewModel();
          m.UserNumber = Convert.ToInt32(Session["UserNumber"]);
          DataTable dt = new DataTable();

          dt = new DataTable();
          string constr = ConfigurationManager.ConnectionStrings["strCnnStore_Data"].ConnectionString;



          dt = new DataTable();

          string strSQL = "SELECT * FROM UserStoreDefinition"
                + " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")";

          using (DataAccess da = new DataAccess(constr))
          {
            dt = da.GetDataTable(strSQL, CommandType.Text);
          }

          // Convert a DataTable to a list. 
          m.lstStoreDefinition = LinqExtensions.ToListof<UserStoreDefinition>(dt);

          dt = new DataTable();

          strSQL = "SELECT distinct StoreOwnerName, StoreOwnerNumber FROM UserStoreDefinition"
            + " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")"
            + " AND (UserStoreDefinition.StoreOwnerNumber <> -1)";

          using (DataAccess da = new DataAccess(constr))
          {
            dt = da.GetDataTable(strSQL, CommandType.Text);
          }
          m.lstStoreOwnerDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);

          dt = new DataTable();

          strSQL = "SELECT distinct StoreRegionName, StoreRegionNumber FROM UserStoreDefinition"
          + " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")"
          + " AND (UserStoreDefinition.StoreRegionNumber <> -1)";

          using (DataAccess da = new DataAccess(constr))
          {
            dt = da.GetDataTable(strSQL, CommandType.Text);
          }
          m.lstStoreRegionDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);

          dt = new DataTable();
          //strSQL = "StoreTypeDefinitions";
          strSQL = "SELECT distinct StoreTypeName, StoreTypeNumber FROM UserStoreDefinition"
          + " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")"
          + " AND (UserStoreDefinition.StoreTypeNumber <> -1)";

          using (DataAccess da = new DataAccess(constr))
          {
            dt = da.GetDataTable(strSQL, CommandType.Text);
          }
          m.lstStoreTypeDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);

          # endregion

          if (ModelState.IsValid)
          {
            string registerToken =
                WebMatrix.WebData.WebSecurity.CreateUserAndAccount(m.UserName, m.Password, new { Email = m.Email }, true);
            SimpleSecurity.WebSecurity.ConfirmAccount(registerToken);

            string confirmationToken =
            WebMatrix.WebData.WebSecurity.GeneratePasswordResetToken(m.UserName);

            dynamic email = new Email("ChngPasswordEmail");
            email.To = m.Email;
            email.UserName = m.UserName;
            email.baseUrl = GetBaseUrl();
            email.ConfirmationToken = confirmationToken;
            email.Send();


          }
          return RedirectToAction("UserList");
        } ````

and this is the Registration View Model that is being inherited by my User Create New View Model:

    {
        [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; }

        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }

    } ````





I am getting an error when I click the create button on this line: ```` string registerToken =



All of the values(m.UserName, m.Password and m.Email) are NULL.

来源:https://stackoverflow.com/questions/56653914/how-to-pass-data-from-a-view-to-the-controller-using-the-webmatrix-websecurity-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!