MODEL COULD NOT BE FOUND IN MVC

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 2057  View(s)
Ratings:
Rate this:

Hi Everyone!

I have included a model and created a view file as well as a controller to direct all of them

public class CreateNewUserModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Email required")]
    [EmailAddress(ErrorMessage = "Not a valid email")]
    public string UserEmail { 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; }

    [Required]
    [Display(Name = "Role")]
    public string UserRole { get; set; }
    public IEnumerable<System.Web.Mvc.SelectListItem> UserRoles { get; set; }
}
On top of the view file

I also have @model CreateNewUserModel

and here is the compilation error I have got

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify

your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'CreateNewUserModel' could not be found (are you missing a using directive or an assembly reference?)

 Source Error:  
Line 32:    
Line 33:    
Line 34:     public class _Page_Views_Account_CreateNewUser_cshtml : System.Web.Mvc.WebViewPage<CreateNewUserModel> {
Line 35:        
Line 36: #line hidden

Thanks in advance

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Model could not be found in MVC

    Hi Ankita Pandey!

    You just need to either fully specify the name of the model type or import the namespace for the model.

    In View page, required namespace and model name when you used model properties.

    e.g.

    @model <namespace>.<class name>

    @model ProjectName.CreateNewUserModel

    or

    @using ProjectName
    @model CreateNewUserModel

    Here "ProjectName" is an example that you replace it in which you defined your model class.

    I hope it resolve your problem.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!