无法在MVC 4中获得特定类别的子类别,并且无法设计类别和子类别模型类的问题 [英] not able to get subcategory of particular category in mvc 4 and designing issue of category and subcategory model class
本文介绍了无法在MVC 4中获得特定类别的子类别,并且无法设计类别和子类别模型类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发MVC中的帮助台系统.
i am working on help desk system in mvc.
我只有一个用于用户和技术人员的主表.
i have only one master table for user and technicians.
这是我的类别课程:
public class Category
{
[Key]
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual ICollection<SubCategory> subCategory { get; set; }//category can have more than 1 category
}
这是我的子类别:
public class SubCategory
{
[Key]
public int SubcategoryId { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual ICollection<TicketInfo> ticketsInfo { get; set; }/to keep track of all tickets under this particular subcategory.
public virtual ICollection<UserDetails> technicianInfo { get; set; }//to keep track of technician and user under this subcategory.
public virtual Category category { get; set; }
}
这是我的usermaster(定义了用户和技术人员)
This is my usermaster(it defines both user and technician)
public class UserDetails
{
public string UserName { get; set; }
[Key]
public int UserId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string PhoneNo { get; set; }
public string EmailId { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
public int SubcategoryId { get; set; }
public int AddressId { get; set; }
public Boolean IsActive { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ICollection<Role> Roles { get; set; }
public virtual SubCategory subCategory { get; set; }
}
现在当我触发dis查询时::
now when i am firing dis query::
public list<Category> FetchTicketDetailsforSubcategory(int categoryId)
{
using (HelpDeskdbContext context = new HelpDeskdbContext())
{
var category = from temp in context.Category where temp.CategoryId == categoryId select temp;
return category;
}
}
它只是显示类别,而不显示该类别下的子类别.
it just show me the category but not subcategory under that category.
在子类别上向我显示此信息: ObjectContext实例已被处置,不能再用于需要连接的操作.
it show me this on subcategory:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
任何人都可以找出我的班级设计出了什么问题吗?
can any one figure out what is wrong with my class design??
推荐答案
尝试一下
context.Category.Where(x=>x.CategoryId == categoryId).SelectMany(x=>x.subCategory).ToList()
这篇关于无法在MVC 4中获得特定类别的子类别,并且无法设计类别和子类别模型类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文