Search code examples
c#asp.net-mvcasp.net-identity

SystemFormatException in RegisterView after adding Username to ASP MVC Identity


I extended the ASP MVC Identity User in my application. Added Username to the model, to all the Viewmodels where it is necesary and also updated my views and Controllers.

Entity:

public class ApplicationUser : IdentityUser
{
   public string Username { get; set; }
   public string Country { get; set; }
   public string City { get; set; }       
   public System.DateTime? BirthDate { get; set; }
...

ViewModel:

public class RegisterViewModel : BaseViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(30,ErrorMessage = "The {0] must be at least {2} characters long.",MinimumLength = 6)]
    [Display(Name = "Username")]
    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; }
}

I updated my database using the nuget packet manager cmd : Update-Database, etc..

The only problem I can't seem to fix is when i add a TextboxFor formgroup item to my registerview

<div class="col-md-8">
    @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
</div>

It gives following error :

System.FormatException occurred
HResult=0x80131537
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
<Cannot evaluate the exception stack trace>

I tried a lot of things to fix this but I can't seem to find a solution.


Solution

  • This line from your sample:

    [StringLength(30,ErrorMessage = "The {0] must be at least {2} characters long.",MinimumLength = 6)]
    

    You have a square bracket after {0. It should be both curly brackets: {0}.