MVC Razor 变量数据

发布于 2024-11-14 23:07:08 字数 1184 浏览 1 评论 0原文

您好,我是 MVC3 Razor 的新手。

我试图显示数据库表中的值。

在控制器中,我将代码编写为

            var test1 = from ed in db.EmpDetails
            join dp in db.Departments on ed.EmpDept equals dp.DeptId
                select new
                {
                    EmpId = ed.EmpId,
                    EmpName = ed.EmpName,
                    EmpDesignation = ed.EmpDesignation,
                    EmpSalary = ed.EmpSalary,
                    EmpUserName =ed.EmpUserName,
                    EmpDept =ed.EmpDept,
                    deptName = dp.deptName
                };

        ViewBag.test = test1; 

并在视图中

               @foreach (var tm in ViewBag.test)
                {

                     string abc [email protected]; 
                     // and the logic
                 }


Here i am getting all the value in the "tm" variable. But when i try to get the particular value of EmpName in a string it is showing the error as "'object' does not contain a definition for 'EmpName'". 

请帮助我解决此错误。 谢谢 桑

Hi i am new to MVC3 Razor .

I was trying to display the values from the database table.

in controller i wrote the code as

            var test1 = from ed in db.EmpDetails
            join dp in db.Departments on ed.EmpDept equals dp.DeptId
                select new
                {
                    EmpId = ed.EmpId,
                    EmpName = ed.EmpName,
                    EmpDesignation = ed.EmpDesignation,
                    EmpSalary = ed.EmpSalary,
                    EmpUserName =ed.EmpUserName,
                    EmpDept =ed.EmpDept,
                    deptName = dp.deptName
                };

        ViewBag.test = test1; 

and in the view

               @foreach (var tm in ViewBag.test)
                {

                     string abc [email protected]; 
                     // and the logic
                 }


Here i am getting all the value in the "tm" variable. But when i try to get the particular value of EmpName in a string it is showing the error as "'object' does not contain a definition for 'EmpName'". 

Please help me to solve this error.
Thanks
san

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-11-21 23:07:08

不幸的是,匿名对象不能与视图一起使用。要么您需要返回一个非匿名的视图,要么返回一个动态对象来查看。

请参阅:http://rhizohm.net/irhetoric/post/2011/05/25/Taking-The-M-out-of-MVC-JSON-The-dynamic-Keyword-LINQ-.aspx

Unfortunately anonymous objects doesn't work with Views. Either you need to return a non-anonymous to view Or return a dynamic object to view.

Refer : http://rhizohm.net/irhetoric/post/2011/05/25/Taking-The-M-out-of-MVC-JSON-The-dynamic-Keyword-LINQ-.aspx

允世 2024-11-21 23:07:08

您不能在视图中使用匿名对象。尝试这样:

var test1 = 
    from ed in db.EmpDetails
    join dp in db.Departments on ed.EmpDept equals dp.DeptId
    select ed;
ViewBag.test = test1; 

然后:

@foreach (var tm in (IEnumerable<Employee>)ViewBag.test)
{
    string abc = tm.EmpName; 
    // and the logic
}

但我个人建议您使用强类型视图而不是 ViewBag:

var test1 = 
    from ed in db.EmpDetails
    join dp in db.Departments on ed.EmpDept equals dp.DeptId
    select ed;
return View(test1); 

并在视图内部:

@model IEnumerable<Employee>
@foreach (var tm in Model)
{
    string abc = tm.EmpName; 
    // and the logic
}

You cannot use anonymous objects in views. Try like this:

var test1 = 
    from ed in db.EmpDetails
    join dp in db.Departments on ed.EmpDept equals dp.DeptId
    select ed;
ViewBag.test = test1; 

and then:

@foreach (var tm in (IEnumerable<Employee>)ViewBag.test)
{
    string abc = tm.EmpName; 
    // and the logic
}

But personally I would recommend you using strongly typed views instead of ViewBag:

var test1 = 
    from ed in db.EmpDetails
    join dp in db.Departments on ed.EmpDept equals dp.DeptId
    select ed;
return View(test1); 

and inside the view:

@model IEnumerable<Employee>
@foreach (var tm in Model)
{
    string abc = tm.EmpName; 
    // and the logic
}
甜扑 2024-11-21 23:07:08

如果我将 linq 结果转换为控制器中的 toList() ,我就可以获取视图中的值。

    Like

var test = (从 db.EmpDetails 中的 p 按 p.EmpName 升序选择 p).ToList();
ViewBag.test = 测试;

    And in the view 

   @foreach (var tm in ViewBag.test)
   {
           int emId = @tm.id;
    }

谢谢....

I am able to get the values in the view if i convert linq result to toList() in controller.

    Like

var test = (from p in db.EmpDetails orderby p.EmpName ascending select p).ToList();
ViewBag.test = test;

    And in the view 

   @foreach (var tm in ViewBag.test)
   {
           int emId = @tm.id;
    }

Thanks....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文