我正在尝试验证某些表单字段,但它总是在Html表单助手上出错。它与模型绑定有关,但我不能让它工作。
下面是我使用FormsCollection对象从表单中获取Form值,然后将提取的值传递给USerSErvice层并验证它们。当我点击表格时,它会在Html.Helpers
上崩溃谢谢!
[在UserController中]
[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();
}
[UserService]
/// <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行:&lt;%= Html.TextBox(“用户名”)%&gt; 第25行:
第26行:
答案 0 :(得分:2)
如果您不想使用内置的模型绑定,那么要使用ModelState.AddModelError进行验证,您还需要模型值。在您的服务层中,每个属性都需要以下两行。
_modelState.AddModelError("Username", "Please provide a username");
_modelState.SetModelError("Username", ValueProvider["Username"]);
还要看看这些链接: