我创建了一个父模型insertBreweryModel.cs:
public class InsertBreweryModel
{
public IList<Breweries> Breweries { get; set; }
public IList<BreweryData> BreweryData { get; set; }
}
在我看来我引用它:
@model BaPP.Models.InsertBreweryModel
现在我成功地可以使用foreach迭代Breweries中的项目,但是我如何才能显示来自其他tablem的数据,其中id与item.id相同?
@foreach (var item in Model.Breweries)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BreweryName)
</td>
<td>@Html.DisplayForModel(Model.BreweryData Pseudo: show data from brewerydata.column where id = item.id
</td>
希望有人可以提供帮助吗?
答案 0 :(得分:3)
对于可能有同样问题的所有人来说,第一个答案是正确的,但在我看来,我错过了其他数据库。这就是我解决它的方式。
我的模特
namespace BaPP.Models
{
public class InsertBreweryModel
{
public Breweries Breweries { get; set; }
public BreweryData BreweryData { get; set; }
}
}
我的控制器
namespace BaPP.Controllers
{
public class BreweryController : Controller
{
private ProjectContext db = new ProjectContext();
//
// GET: /Default1/
public ActionResult Index()
{
var viewModel = from o in db.Breweries join o2 in db.BreweryData on o.BreweryId equals o2.BreweryId where o.BreweryId.Equals(o2.BreweryId) select new InsertBreweryModel { Breweries = o, BreweryData = o2};
return View(viewModel);
}
我的观点
@model IEnumerable<BaPP.Models.InsertBreweryModel>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Breweries.BreweryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BreweryData.DescriptionText)
</td>
对于两个评论再次thx: - )
答案 1 :(得分:0)
试试这个:
@foreach (var item in Model.Breweries)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BreweryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.tableName.tableFieldname)
</td>
</tr>
}
答案 2 :(得分:0)
尝试使用类似的东西
@foreach (var item in Model.Breweries)
{
@Html.DisplayFor(model => item.FieldForBreweries)
foreach (var item1 in Model.BreweryData.Where(x => x.Id == item.Id))
{
@Html.DisplayForModel(item1)
}
}