简体   繁体   中英

How to access data from query without a model in ASP.NET 5 MVC

I am trying to execute a query inside a controller using EF7 and to display data in a table.

Here are my models:

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

and here is the join query in the controller:

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

In the view I have a foreach loop that is able to extract correctly each row but not the columns. Error: 'object' does not contain a definition for '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>

I added also an extra model for the result but still without success. Error is 'ManageTagTableModel' does not contain a public definition for '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();
    }
}

The question is: how can I access the 'new' generic object properties if possible without the need for additional models?

You need to project your query into new instances of 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());

and then in the view

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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