0
votes

I have got this error when trying to navigate to login view in my browser in a project that I have been working on. enter image description here

I have got a username property in my AccountViewModel

 public class LoginViewModel
{
    [Required(ErrorMessageResourceName = "UserNameRequired")]
    [Display(Name = "UserName")]
    public string UserName { get; set; }

    [Required(ErrorMessageResourceName = "PasswordRequired")]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]

    public string Password { get; set; }

    [Display(Name = "RememberMe")]
    public bool RememberMe { get; set; }
}

How can I avoid this problem? Can anyone suggest me a solution?

After I have changed the required attribute like this

        [Required(ErrorMessageResourceType = typeof(InitialCreate1),ErrorMessageResourceName = "UserNameInvalid", ErrorMessage = null)]
    [Display(Name = "UserName")]
    public string UserName { get; set; }

I have got this new error

Server Error in '/' Application.

Either ErrorMessageString or ErrorMessageResourceName must be set, but not both.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Either ErrorMessageString or ErrorMessageResourceName must be set, but not both.

Source Error:

Line 17: @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) Line 18: Line 19: @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) Line 20: @Html.ValidationMessageFor(m => m.UserName) Line 21:

What can I do about this? Please help me?

1

1 Answers

2
votes

The first error message is saying that you need to provide a resource Type in addition to the resource name. For example, if you have a resource called Validation.resx then you would need to declare the resource type on the attribute.

[Required(
    ErrorMessageResourceName = "UserNameRequired",
    ErrorMessageResourceType = typeof(Validation))]
[Display(Name = "UserName")]
public string UserName { get; set; }

The resource file might look something like this:

enter image description here

The second error message is saying that you must declare ErrorMessage or ErrorMessageResourceName, but not both. To fix the error you need to remove ErrorMessage = null from your attribute declaration.

If in fact you don't have a resource file then you might find it easier to declare the error message on the attribute directly.

[Display(Name = "UserName")]
[Required(ErrorMessage = "Please enter your username.")]
public string UserName { get; set; }