【问题标题】:Need help with formcollection model binding在 formcollection 模型绑定方面需要帮助
【发布时间】:2009-12-05 17:05:21
【问题描述】:

我正在尝试验证一些表单字段,但它总是在 Html 表单助手上出错。它与模型绑定有关,但我无法让它工作。

下面发生的事情是我使用 FormsCollection 对象从表单中获取表单值,然后将提取的值传递给 USerSErvice 层并验证它们。当我单击表单时,它会在 Html.Helpers 上崩溃

谢谢!

[在用户控制器中]

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection formValues)
    {
        // Get form values
        string username = formValues["Username"];
        string email = formValues["Email"];
        string confirmEmail = formValues["ConfirmEmail"];
        string password = formValues["Password"];
        string confirmPassword = formValues["ConfirmPassword"];
        bool themeRole = Convert.ToBoolean(formValues["ThemeRole"]);

        bool valid = _service.CreateUser(username, email, confirmEmail, password, confirmPassword, themeRole);

        // If user was created then
        if (valid)
        {
            ViewData["Success"] = true;
        }
        else
        {
            ViewData["Success"] = false;
        }
          
        return View();
    }

[用户服务]

区域创建用户

    /// <summary>
    /// Create User
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public bool CreateUser(string username, string email, string confirmEmail, string password, string confirmPassword, bool themeRole)
    {           
        // validate values from form
        if (String.IsNullOrEmpty(username) == true)
        {
            _modelState.AddModelError("Username", "Please provide a username");
        }

        if (String.IsNullOrEmpty(password) == true)
        {
            _modelState.AddModelError("Password", "Please provide a password");
        }
        else if (password.Length < 6)
        {
            _modelState.AddModelError("Password", "Minimum length for password is 6 characters");
        }

        if (String.IsNullOrEmpty(confirmPassword) == true)
        {
            _modelState.AddModelError("ConfirmPassword", "Please confirm your password in the Confirm Password field");
        }
        else if (!confirmPassword.Equals(password) == true)
        {
            _modelState.AddModelError("ConfirmPassword", "Passwords do not match");
        }

        if (String.IsNullOrEmpty(email) == true)
        {
            _modelState.AddModelError("Email", "Please provide an email address");
        }

        if (String.IsNullOrEmpty(confirmEmail) == true)
        {
            _modelState.AddModelError("ConfirmEmail", "Please confirm your email addres in the confirm email field");
        }
        else if (!confirmEmail.Equals(email) == true)
        {
            _modelState.AddModelError("ConfirmEmail", "Emails do not match");
        }

        // if all is good - create account otherwise it fails
        if (_modelState.IsValid == true)
        {
            // Encode password
            byte[] generatedSalt = Utility.GeneratePasswordSalt(new byte[16]);
            string saltString = Encoding.Unicode.GetString(generatedSalt);
            string passwordSalt = Convert.ToBase64String(generatedSalt);
            string newHashedPassword = Utility.EncodePassword(password, passwordSalt);

            // Update user object with new user information
            User newUser = new User();
            newUser.Username = username;
            newUser.Password = newHashedPassword;
            newUser.PasswordSalt = passwordSalt;
            newUser.Email = email;
            newUser.Role = themeRole;

            // Add user
            _repository.AddUser(newUser);
            
            return true;
        }
        else
        {
            return false;
        }
    }

[错误信息]

 Object reference not set to an instance of an object.

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

来源错误:

第 22 行:

第 23 行:用户名: 第 24 行: 第 25 行:

第 26 行:

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    如果您不想使用内置模型绑定,那么要使用 ModelState.AddModelError 进行验证,您还需要模型值。在您的服务层中,每个属性都需要以下 2 行。

    _modelState.AddModelError("Username", "Please provide a username");
    _modelState.SetModelError("Username", ValueProvider["Username"]);
    

    也看看这些链接:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2020-09-30
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多