【问题标题】:.net MVC passing linq data from controller to view.net MVC 将 linq 数据从控制器传递到视图
【发布时间】:2013-05-23 12:37:00
【问题描述】:

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

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

控制器:

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

查看:

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

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

控制器:

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
}

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

任何帮助都会很棒。

谢谢。

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

【问题讨论】:

  • 实际上,我不明白为什么您的 first 代码有效。看起来它为每个a 显示a.Value.Value
  • @justin 你是对的,第一个代码不起作用。现在更新...

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


【解决方案1】:

像这样使用某物

ViewBag.qualification = new SelectList(db.Lookups.Where(x => x.lookup_type == "Qualification"), "lookup_content", "lookup_content");

【讨论】:

    【解决方案2】:

    这是因为您已经选择了Value,而Value 没有Value 这样的属性。您应该更改控制器:

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

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

    将视图更改为

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

    并更改您的控制器:

    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 模式传递给视图呢?

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

    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>
    

    【讨论】:

    • 其实我想写下面的linq查询,选择多于1个字段:from a in Connection.Db.Authorities select new { Value = a.Value, TypeCode = a.TypeCode, Return = Calculate (a.返回) }
    • 是的 Dmytro,你是对的,它现在可以工作了。非常感谢...问题是,正如您所说,我应该创建一个视图模型类。而且,作为对您问题的回答,我想发送多个查询结果以供查看,因此我使用 ViewBag 而不是 Model。
    • @isa,发送多个查询结果您可以创建更复杂的模型。例如查看我的更多编辑。
    • 这正是我一直在寻找的。非常感谢。您清楚地定义了所需的一切。
    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多