简体   繁体   English

在 ASP.NET Core MVC 视图中使用动态 object

[英]Using dynamic object in ASP.NET Core MVC view

Let me start by saying that I know there are other ways to do this and I have already successfully done it other ways.首先让我说我知道还有其他方法可以做到这一点,而且我已经成功地通过其他方式做到了。 But I got to wondering how to use @model IEnumerable<dynamic> in a view.但我想知道如何在视图中使用@model IEnumerable<dynamic>

I have tried this in the controller:我在 controller 中试过这个:

var result = from p in _db.People
             join ps in _db.PersonSport on p.PersonId equals ps.PersonIdfk
             join s in _db.Sports on ps.SportCodeFk equals s.SportCode
             orderby s.SportName
             where p.LastName == "Orr"
             select new { p.FirstName, p.LastName, s.SportName };
return View(result);

With this in the view:鉴于此:

@model IEnumerable<dynamic>
<h4>Sports played by @Model.First().FirstName @Model.First().LastName:</h4>
   @foreach (var item in Model) {
       @item.SportName;<br />
    }

And I get a runtime error我得到一个运行时错误

RuntimeBinderException: 'object' does not contain a definition for 'FirstName' RuntimeBinderException:“对象”不包含“名字”的定义

A breakpoint at the <h4> shows that model has the data: <h4>处的断点显示 model 具有以下数据:

在视图中显示扩展模型的自动变量

I tried using creating result as a new ExpandoObject() and got the same error.我尝试使用创建结果作为new ExpandoObject()并得到相同的错误。

Am I way off base here or attempting something impossible?我是在这里偏离基地还是在尝试一些不可能的事情? I was thinking of a use case where you have many table columns but are only extracting a few.我在考虑一个用例,您有许多表列但只提取了一些列。 Yes, I know I could create a class and do it that way, or send the data in ViewModel/ViewData, but was wondering if this was an option.是的,我知道我可以创建一个 class 并这样做,或者在 ViewModel/ViewData 中发送数据,但想知道这是否是一个选项。

The short answer is that using anonymous types is not supported , however, there is a workaround , you can use an ExpandoObject .简短的回答是不支持使用匿名类型,但是, 有一种解决方法,您可以使用ExpandoObject

The whole working demo is like below:整个工作演示如下:

  1. Custom ToExpando method:自定义ToExpando方法:
public static class Extension
{
    public static IEnumerable<dynamic> ToExpando(this IEnumerable<object> anonymousObject)
    {
        IList<dynamic> list = new List<dynamic>();

        foreach (var item in anonymousObject)
        {
            IDictionary<string, object> anonymousDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(item);
            IDictionary<string, object> expando = new ExpandoObject();

            foreach (var nestedItem in anonymousDictionary)
                 expando.Add(nestedItem);

            list.Add(expando);
        }

        return list.AsEnumerable();
    }
}
  1. View:看法:
    @model IList<dynamic>
    <table class="table">
        <thead>
            <tr>
                <th>FirstName</th>
                <th>LastName</th>
                <th>SportName</th>
            </tr>
        </thead>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.FirstName
                </td>
                <td>
                    @item.LastName
                </td> 
                <td>
                    @item.SportName
                </td>
            </tr>
    
        }
    </table>
  1. Controller: Controller:
public IActionResult Index()
{
    var result = (from p in _db.People
             join ps in _db.PersonSport on p.PersonId equals ps.PersonIdfk
             join s in _db.Sports on ps.SportCodeFk equals s.SportCode
             orderby s.SportName
             where p.LastName == "Orr"
             select new { p.FirstName, p.LastName, s.SportName }
             ).ToExpando().ToList();
    return View(result);
}

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM