.net MVC将linq数据从控制器传递到视图

时间:2021-02-04 19:42:18

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:

如果我这样做它有效:

Controller:

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

View:

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

But the following code does not work:

但是以下代码不起作用:

Controller:

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

View:

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

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

它为视图文件提供“item不包含Value的定义”。

Any help would be great.

任何帮助都会很棒。

Thank you.

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

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

2 个解决方案

#1


4  

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

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

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

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

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

OR change the view to

或者将视图更改为

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

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

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

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

And change your 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;

and in your view you will be able to use:

在您的视图中,您将能够使用:

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

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?

另外,我有一个问题要问你。为什么使用ViewBag将数据从控制器传递到视图?为什么不根据MVC模式使用Model传递这些数据?

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

And change the 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);

and in view you can use:

在视图中你可以使用:

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

#2


0  

use sth like this

像这样使用......

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

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

#1


4  

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

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

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

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

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

OR change the view to

或者将视图更改为

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

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

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

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

And change your 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;

and in your view you will be able to use:

在您的视图中,您将能够使用:

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

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?

另外,我有一个问题要问你。为什么使用ViewBag将数据从控制器传递到视图?为什么不根据MVC模式使用Model传递这些数据?

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

And change the 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);

and in view you can use:

在视图中你可以使用:

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

#2


0  

use sth like this

像这样使用......

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

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