使用模型时无法添加视图

时间:2018-01-09 13:00:39

标签: c# asp.net-mvc-4

我的应用程序中有以下模型,但是当我尝试使用它在控制器上生成视图时,我收到错误"无法检索Jop_Offers_Website.Models.JobRequest的元数据":

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using WebApplication3.Models;

namespace Jop_Offers_Website.Models
{
    public class JobRequest 
    {
        public int Id { get; set; }
        public string Message { get; set; }
        public DateTime ApplyDate { get; set; }

        public int JobId { get; set; }
        public string UserId { get; set; }

        public virtual Jobs job { get; set; }
        public virtual ApplicationUser user { get; set; }
    }
}

Error dialog

当我使用其他模型添加视图时,或者如果我注释掉jobuser属性,则视图会成功生成。

Jobs模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Jop_Offers_Website.Models
{
    public class Jobs
    {
        //id to be primary key of Jobs table
        public int Id { get; set; }

        //job title
        [Required]
        [Display (Name ="أسم الوظيفة")]
        public string JobTitle { get; set; }

        //job description
        [Required]
        [Display(Name ="وصف الوظيفة ")]
        public string JobDescription { get; set; }

        //jop image
        [Display(Name ="صورة الوظيفة ")]
        public string JobImage { get; set; }

        //id of categories to relate to Job category Type 1 to many relationship
        //[Required]
        public int CategoryId { get; set; }

        //object of category to detect job category 
        public virtual categories category { get; set; }

    }
}

ApplicationUser型号:

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace WebApplication3.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser

    {
        public string UserType { get; set; }
        public string Neabouring { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("JobConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.categories> categories { get; set; }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.Jobs> Jobs { get; set; }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.RoleViewModel> RoleViewModels { get; set; }


        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.JobRequest> JobRequests { get; set; }

    }
}

用于添加视图的任何模型,但我无法使用JobRequest模型添加视图 当我使用JobRequest模型而没有ApplicationDbContext时,视图会成功生成。

0 个答案:

没有答案