I created a Model to do registration on my Website, and add some validations to it. When the user inputs a data, the data will be checked for validity using ModelState.IsValid. But when I input a correct data, it always be invalid. So I decided to debug
my program and found that there are an extra key in my ModelState.
For my model, I have UserName, Email, Password, ConfirmPassword, SecretQuestion and SecretAnswer (6 elements)
But when I debug, in ModelState, I found 7 elements, they are UserName, Email, Password, ConfirmPassword, SecretQuestion SecretAnswer and the last one is "".
I don't know why there is an extra "" in my ModelState, and I checked that it was the source of my error. Does anybody know why it appears and how to fix it ?
Below I enclose my source to make it clearer
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
[PropertiesMustNotMatch("UserName", "Password", ErrorMessage = "The username must not be the same as password.")]
[PropertiesMustNotMatch("SecretQuestion", "SecretAnswer", ErrorMessage = "Secret question must not be the same with secret answer.")]
public class RegisterModel
{
[Required]
[DisplayName("User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[DisplayName("Email address")]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DisplayName("Secret Question")]
public string SecretQuestion { get; set; }
[Required]
[DisplayName("Secret Answer")]
public string SecretAnswer { get; set; }
};
I am sorry if I posted in wrong forum, last time my post was moved from MVC to here, and I thought when I use ModelState, which is about data validation, I think this is also the place.
Anyway, back to the topic, here is my action and view. It may not be completed yet, but it's better to debug first than to write a lot of code and test..
Below is the action in my controller:
public ActionResult Register(RegisterModel model)
{
try
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
User u = new User();
if (TryUpdateModel(u))
{
repo.Add(u);
return RedirectToAction("Index", "User");
}
else
{
return View("Error");
}
}
else
{
TempData["Message"] = "Model is not valid";
return View("Error");
}
}
catch
{
return View();
}
}
I just made a little change to the automatically generated view from the visual studio, such as omitting the ID field, which is type of serial (autoincrement), and change TextBoxFor password to PasswordFor
Strangely, when I tried again today, the ModelState count is going normal, which is 6, therefore I could not reproduce the issue. Sorry for that..If in the future I get this problem, I will try your solution.
robinchsz
Member
3 Points
11 Posts
ModelState has an extra key which is empty ?
Sep 11, 2010 11:46 AM|LINK
Hi everybody,
I created a Model to do registration on my Website, and add some validations to it. When the user inputs a data, the data will be checked for validity using ModelState.IsValid. But when I input a correct data, it always be invalid. So I decided to debug my program and found that there are an extra key in my ModelState.
For my model, I have UserName, Email, Password, ConfirmPassword, SecretQuestion and SecretAnswer (6 elements)
But when I debug, in ModelState, I found 7 elements, they are UserName, Email, Password, ConfirmPassword, SecretQuestion SecretAnswer and the last one is "".
I don't know why there is an extra "" in my ModelState, and I checked that it was the source of my error. Does anybody know why it appears and how to fix it ?
Below I enclose my source to make it clearer
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")] [PropertiesMustNotMatch("UserName", "Password", ErrorMessage = "The username must not be the same as password.")] [PropertiesMustNotMatch("SecretQuestion", "SecretAnswer", ErrorMessage = "Secret question must not be the same with secret answer.")] public class RegisterModel { [Required] [DisplayName("User name")] public string UserName { get; set; } [Required] [DataType(DataType.EmailAddress)] [DisplayName("Email address")] public string Email { get; set; } [Required] [ValidatePasswordLength] [DataType(DataType.Password)] [DisplayName("Password")] public string Password { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("Confirm password")] public string ConfirmPassword { get; set; } [Required] [DisplayName("Secret Question")] public string SecretQuestion { get; set; } [Required] [DisplayName("Secret Answer")] public string SecretAnswer { get; set; } };[URL=http://img37.imageshack.us/i/errorvh.jpg/][IMG]http://img37.imageshack.us/img37/3415/errorvh.jpg[/IMG][/URL]
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: ModelState has an extra key which is empty ?
Sep 12, 2010 04:16 AM|LINK
1. this question belongs to MVC thread
http://forums.asp.net/1146.aspx
2. Could you show your action and your view?
robinchsz
Member
3 Points
11 Posts
Re: ModelState has an extra key which is empty ?
Sep 12, 2010 06:13 AM|LINK
Hi ignatandrei, thank you for your response.
I am sorry if I posted in wrong forum, last time my post was moved from MVC to here, and I thought when I use ModelState, which is about data validation, I think this is also the place.
Anyway, back to the topic, here is my action and view. It may not be completed yet, but it's better to debug first than to write a lot of code and test..
Below is the action in my controller:
public ActionResult Register(RegisterModel model) { try { // TODO: Add insert logic here if (ModelState.IsValid) { User u = new User(); if (TryUpdateModel(u)) { repo.Add(u); return RedirectToAction("Index", "User"); } else { return View("Error"); } } else { TempData["Message"] = "Model is not valid"; return View("Error"); } } catch { return View(); } }and here is my view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<OmnipresentWebService.Models.RegisterModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Register </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Register</h2> <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <div class="editor-label"> <%: Html.LabelFor(model => model.UserName) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.UserName) %> <%: Html.ValidationMessageFor(model => model.UserName) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Email) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Email) %> <%: Html.ValidationMessageFor(model => model.Email) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Password) %> </div> <div class="editor-field"> <%: Html.PasswordFor(model => model.Password) %> <%: Html.ValidationMessageFor(model => model.Password) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.ConfirmPassword) %> </div> <div class="editor-field"> <%: Html.PasswordFor(model => model.ConfirmPassword) %> <%: Html.ValidationMessageFor(model => model.ConfirmPassword) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.SecretQuestion) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.SecretQuestion) %> <%: Html.ValidationMessageFor(model => model.SecretQuestion) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.SecretAnswer) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.SecretAnswer) %> <%: Html.ValidationMessageFor(model => model.SecretAnswer) %> </div> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%: Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>I just made a little change to the automatically generated view from the visual studio, such as omitting the ID field, which is type of serial (autoincrement), and change TextBoxFor password to PasswordForignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: ModelState has an extra key which is empty ?
Sep 12, 2010 09:35 PM|LINK
Please change your actionresult into :
and tell what appears in your
Html.ValidationSummary(true)
robinchsz
Member
3 Points
11 Posts
Re: ModelState has an extra key which is empty ?
Sep 13, 2010 05:39 AM|LINK
Thank you for your response,
Strangely, when I tried again today, the ModelState count is going normal, which is 6, therefore I could not reproduce the issue. Sorry for that..If in the future I get this problem, I will try your solution.
Anyway thanks very much..