mvc4在列表中显示分类的名称

chinatefl 2013-05-29 01:39:22
代码如下:
 public ActionResult Index()
{
var QuestionInfoList = db.QuestionInfoes.Include(a => a.Q_Category).Include(a => a.Q_Type);
return View(QuestionInfoList.ToList());
}

QuestionInfoes 是主表的实例化 Q_Category 和 Q_Type 都是 id name这样的代码表,也在model定义了,为何显示列表报错呢?
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinatefl 2013-05-29
  • 打赏
  • 举报
回复
那该怎么写呢????
chinatefl 2013-05-29
  • 打赏
  • 举报
回复
视图代码:
@foreach (var item in Model) {
    <tr>
    <td>@Html.DisplayFor(modelItem => item.Q_Type.Q_TypeName)</td>
    <td>@Html.DisplayFor(modelItem => item.Q_Category.Q_CategoryName)</td>
        <td>
            @Html.DisplayFor(modelItem => item.Q_Title)
        </td>           
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Q_Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Q_Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Q_Id })
        </td>
    </tr>
}
chinatefl 2013-05-29
  • 打赏
  • 举报
回复
模型定义如下:
    [Table("Question")]
    public class QuestionInfo
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Q_Id { get; set; }

        
        [Display(Name = "Q_Title")]
        public string Q_Title { get; set; }

        public QuestionType Q_Type { get; set; }
        public QuestionCategory Q_Category { get; set; }   

        
    }

    [Table("QuestionType")]
    public class QuestionType
    {
        [Key]
        public int Q_TypeID { get; set; }
        public string Q_TypeName { get; set; }
    }

     [Table("QuestionCategory")]
    public class QuestionCategory
    {
        [Key]
        public int Q_CategoryID { get; set; }
        public string Q_CategoryName { get; set; }
        public int Q_CategoryParentID { get; set; }
    }
EnForGrass 2013-05-29
  • 打赏
  • 举报
回复
是不是db.QuestionInfoes中的Q_CategoryID在Q_Category中没得啊
chinatefl 2013-05-29
  • 打赏
  • 举报
回复
还是报同样的错!!!
EnForGrass 2013-05-29
  • 打赏
  • 举报
回复
这样呢 var QuestionInfoList = db.QuestionInfoes.Include("Q_Category").Include("Q_Type"); return View(QuestionInfoList.ToList());

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧