【问题标题】:How to access data from query without a model in ASP.NET 5 MVC如何在没有 ASP.NET 5 MVC 中的模型的情况下从查询中访问数据
【发布时间】:2016-02-11 00:54:35
【问题描述】:

我正在尝试使用 EF7 在控制器内执行查询并在表中显示数据。

这是我的模型:

public class Resources
{
    public int id { get; set; }
    public string name { get; set; }
    public string descr { get; set; }
    public string resourcePath { get; set; }
    public string method { get; set; }
    public string format { get; set; }

    public virtual ICollection<MeasureTags> Tags { get; set; }
}

public class MeasureSets
{
    public int id { get; set; }
    public string name { get; set; }
    public string descr { get; set; }
    public string userName { get; set; }
    public bool isPublic { get; set; }

    public virtual ICollection<MeasureTags> Tags { get; set; }
}

public class MeasureTags
{
    public int id { get; set; }
    public string name { get; set; }
    public string userName { get; set; }
    public bool isPublic { get; set; }
    public int sortOrder { get; set; }

    [ForeignKey("MeasureSets")]
    public int msId { get; set; }
    public virtual MeasureSets ms { get; set; }

    public virtual Resources res { get; set; }
}

这是控制器中的连接查询:

//get users measure sets
var m_sets = from t in _context.MeasureTags
             join s in _context.MeasureSets on t.ms.id equals s.id 
             where s.userName == User.Identity.Name
             select new {
                 id = t.id,
                 set = s.name,
                 tag = t.name,
                 res = t.res.name
             };

return View(m_sets.ToArray());

在视图中,我有一个 foreach 循环,它能够正确提取每一行,但不能正确提取列。错误:'object' 不包含 'id' (item.id) 的定义

<tbody>
@foreach (var item in Model)
{
   string tagid = "tag" + item.id;
       <tr>
        <td><input type="checkbox" checked id="@tagid"/></td>
        <td>@item.set</td>
        <td>@item.tag</td>
        <td>@item.res</td>
       </tr>
    }
}
</tbody>

我还为结果添加了一个额外的模型,但仍然没有成功。错误是“ManageTagTableModel”不包含“GetEnumerator”的公共定义

public class TagTable
{
    public int id { get; set; }
    public string set { get; set; }
    public string tag { get; set; }
    public string res { get; set; }
}

public class ManageTagTableModel : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        return ((IEnumerable)TagTable).GetEnumerator();
    }
}

问题是:如果可能的话,我如何在不需要额外模型的情况下访问“新”通用对象属性?

【问题讨论】:

  • 多亏了 Stephen Muecke,我才得以运行。但是,如果有一个选项可以在没有模型的情况下访问通用对象的属性,我仍然在徘徊..

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

您需要将查询投射到 TagTable 的新实例中

var m_sets = from t in _context.MeasureTags
             join s in _context.MeasureSets on t.ms.id equals s.id 
             where s.userName == User.Identity.Name
             select new TagTable {
                 id = t.id,
                 set = s.name,
                 tag = t.name,
                 res = t.res.name
             };
return View(m_sets.ToArray());

然后在视图中

@model IEnumerable<TagTable>
@foreach (var item in Model)
{
    <tr>
        <td>@item.set</td>
        ....

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多