我们可以在不使用模型的情况下在视图中显示linq结果 [英] Can we display linq result in view without using model

查看:79
本文介绍了我们可以在不使用模型的情况下在视图中显示linq结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I was trying to display the result without model(CLASS),i already have a department and Employee class ,now to show their joined result I don,t want to make another class which contains EmpName and Department Name property







模型




IN MODEL

public class EmpDetails
    {     
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
         public int DepartmentID { get; set; }
}
 public class Department
{
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
        public string Location { get; set; }
}



在控制器中


In Controller

 var q = (from ED in cc.EmpDetails
join DT in cc.Department
on ED.DepartmentID equals DT.DepartmentID
select new {
ED.Name,
DT.DepartmentName
});

ViewBag.ListData = q;



查看


In View

foreach (var item in ViewBag.ListData)
       {
           <div>
               @item.Name
           </div>
        <div>
               @item.DepartmentName
           </div>
       }



我希望不使用模型完成



< b>我尝试了什么:



我尝试为empName和部门名称制作一个新模型,这是成功的,

但我想这样做没有模型类


I want it to be done without using model

What I have tried:

I tried by making a new model for empName and department name which was succesfull,
but i want to do it without model class

推荐答案

检查这个



c# - 具有匿名类型模型类的razor视图。有可能的? [ ^ ]

在ASP.NET MVC中使用匿名类型集合绑定视图 [ ^ ]
check this

c# - razor view with anonymous type model class. It is possible? [^]
Binding views with Anonymous type collection in ASP.NET MVC [^]


这篇关于我们可以在不使用模型的情况下在视图中显示linq结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆