如何访问从查询数据没有一个模型在ASP.NET MVC 5 [英] How to access data from query without a model in ASP.NET 5 MVC
本文介绍了如何访问从查询数据没有一个模型在ASP.NET MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用EF7执行控制器内的查询,并在表格中显示的数据。
I am trying to execute a query inside a controller using EF7 and to display data in a table.
下面是我的模型:
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());
在认为我有一个foreach循环,能够正确地提取每行而不是列。错误:对象不包含ID(item.id)
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>
我还添加了一个额外的模型结果,但仍然没有成功。误差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();
}
}
现在的问题是:我怎么可以访问新的通用对象,如果可能的属性,而不需要额外的模型
The question is: how can I access the 'new' generic object properties if possible without the need for additional models?
推荐答案
您需项目查询到 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>
....
这篇关于如何访问从查询数据没有一个模型在ASP.NET MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文