asp.net - .net MVC 将 linq 数据从 Controller 传递到 View

标签 asp.net asp.net-mvc-3 linq razor

我正在尝试将数据从 Controller 传递到 View 。我在网上搜索过但找不到解决方案。

如果我这样做它会起作用:

Controller :

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

查看:

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

但是下面的代码不起作用:

Controller :

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
}

它为 View 文件提供“项目不包含值的定义”。

任何帮助都会很棒。

谢谢。

-编辑:更新了第二个 Controller linq 查询。并更正了第一个 Controller linq 查询。

最佳答案

这是因为您已经选择了Value,并且Value没有Value这样的属性。您应该更改 Controller :

var yyy =(从 Connection.Db.Authorities 中的 a 选择 a.Value);

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

将 View 更改为

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

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

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

并更改您的 Controller :

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 将数据从 Controller 传递到 View ?为什么不按照 MVC 模式使用 Model 将这些数据传递给 View 呢?

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

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; }
        }

并更改 Controller :

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);

并且在 View 中您可以使用:

@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>

关于asp.net - .net MVC 将 linq 数据从 Controller 传递到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793767/

相关文章:

javascript - 如何调用 Controller 方法并更新 View ?

c# - 最终用户希望向他的网站动态添加语言

c# - 如何找到同一个对象在列表中出现了多少次,然后找到出现次数最多的对象的属性

asp.net - SQL Server : How to select multiple columns with 1 column being distinct?

asp.net - MVC 4 - 在局部 View 中使用不同的模型

c# - ASP.NET Identity 记住登录页面上的电子邮件地址

c# - 如何比较两个数据表的单元格值

c# - 当我在 Javascript 中禁用它时,ASP.NET 组合框的值似乎发生了变化

c# - 有没有办法在 ASP.NET MVC3 中将编辑器和显示模板与 Dictionary<K,V> 一起使用?

c# - 如何使用 LINQ 删除没有其子节点的 Xelement?