none
在asp.net mvc5 中查询数据问题. RRS feed

  • 问题

  • 我想在控制器中做一个查询,然后在表格中显示,使用的是EF7.

    Model代码:

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

    }

    控制器查询代码:

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

    View端代码:

     

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

     

    报错:” 'object' does not contain a definition for 'id' (item.id)”

     

    我又新增了一个扩展类如下,还是报错,错误是:'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();

        }

    }

    2016年2月13日 14:42

答案

  • 你好,

    我建议你把Linq 的查询结果赋到一个新的TabTable对象:

    ar 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年2月14日 14:17
    版主