.net MVC从控制器传递linq数据到视图 [英] .net MVC passing linq data from controller to view

查看:80
本文介绍了.net MVC从控制器传递linq数据到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从控制器传递到视图.我已经在网上搜索了,但找不到解决方案.

I am trying to pass data from controller to view. I have searched the web but could not find a solution.

如果我这样做,它将起作用:

if I do this it works:

控制器:

var yyy = (from a in Connection.Db.Authorities select a) ;
ViewBag.data = yyy;

查看:

@foreach(var item in ViewBag.data)
{
    @item.Value
}

但是以下代码不起作用:

But the following code does not work:

控制器:

var yyy = (from a in Connection.Db.Authorities select new {Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)}) ;
ViewBag.data = yyy;

查看:

@foreach(var item in ViewBag.data)
{
    @item.Value
}

它为视图文件提供了项目不包含值的定义".

It gives "item does not contain a definition for Value" for the view file.

任何帮助都会很棒.

谢谢.

-更新了第二个控制器linq查询.并更正了第一个控制器linq查询.

-edited: updated the second controller linq query. and corrected the first controller linq query.

推荐答案

这是因为您已经选择了Value,并且Value不具有Value这样的属性.您应该更改控制器:

It's because You already select Value and Value has no such property as Value. You should change in controller:

var yyy = (from a in Connection.Db.Authorities select a.Value);

var yyy = (from a in Connection.Db.Authorities select a);

OR 将视图更改为

@foreach(var item in ViewBag.data)
{
    @item
}

//////////////////////////////////////////////////编辑//////////////////////////////////////////////////
比你不应该使用匿名对象.您应该创建ViewModelClass.例如:

//////////////////////////////////////////////// EDITS ////////////////////////////////////////////////
Than You should not use anonymous object. You should create ViewModelClass. For Example:

public class AuthoritiesViewModel
        {
            public string Value { get; set; }
            public string TypeCode { get; set; }
            public string Return { get; set; }
        }

并更改您的控制器:

var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});
ViewBag.data = yyy;

,您将可以使用:

<table>
    <tr>
         <th>Value</th>
         <th>TypeCode</th>
         <th>Return</th>
    </tr>
@foreach(AuthoritiesViewModel item in ViewBag.data)
{
    <tr>
         <td>@item.Value<td>
         <td>@item.TypeCode<td>
         <td>@item.Return<td>
    </tr>
}
</table>

另外,我有一个问题要问你.为什么使用ViewBag将数据从控制器传递到视图?为什么不使用Model来按照MVC模式传递这些数据以进行查看?

Also, I have a question to You. Why do You use ViewBag to pass data from controller to view? Why don't You use Model to pass these data to view according to MVC pattern?

//////////////////////////////////////////////////更多编辑/////////////////////////////////////////////////
发送多个查询结果您可以创建更复杂的模型.例如:

//////////////////////////////////////////////// MORE EDITS ////////////////////////////////////////////////
To send more than one query result You can create more complex model. For example:

public class AuthoritiesViewModel
        {
            public string Value { get; set; }
            public string TypeCode { get; set; }
            public string Return { get; set; }
        }

        public class AnotherQueryViewModel
        {
            public string AnotherQueryValue { get; set; }
            public string AnotherQueryTypeCode { get; set; }
            public string AnotherQueryReturn { get; set; }
        }

        public class ModelClass
        {
            IEnumerable<AuthoritiesViewModel> Authorities { get; set; }
            IEnumerable<AnotherQueryViewModel> AnotherQueryResults { get; set; }
        }

并更改控制器:

var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});

// do your another select
var zzz = (from smthing select new AnotherQueryViewModel ...)

// create model instance
ModelClass model = new ModelClass() 
{
    Authorities = yyy.AsEnumerable(),
    AnotherQueryResults = zzz..AsEnumerable()
}

// return view with model
return View("view", model);

在视图中您可以使用:

@model ModelClass

@*display first query result*@
<table>
    <tr>
         <th>Value</th>
         <th>TypeCode</th>
         <th>Return</th>
    </tr>
@foreach(AuthoritiesViewModel item in Model.Authorities)
{
    <tr>
         <td>@item.Value<td>
         <td>@item.TypeCode<td>
         <td>@item.Return<td>
    </tr>
}
</table>

@*display second query result*@
<table>
    <tr>
         <th>Another Query Value</th>
         <th>Another Query TypeCode</th>
         <th>Another Query Return</th>
    </tr>
@foreach(AnotherQueryViewModel item in Model.AnotherQueryResults)
{
    <tr>
         <td>@item.AnotherQueryValue<td>
         <td>@item.AnotherQueryTypeCode<td>
         <td>@item.AnotherQueryReturn<td>
    </tr>
}
</table>

这篇关于.net MVC从控制器传递linq数据到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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