我有一個問題,我需要將多個表返回到視圖。截至目前,我的索引操作結果只能獲得一個表,但這需要修改。MVC 5返回多個表來索引行動結果
下面是控制器代碼:
public class ResidentialBuildingController : Controller
{
private BIRDSResidentialEntities db = new BIRDSResidentialEntities();
// GET: ResidentialBuilding
public ActionResult Index()
{
return View(db.ResidentialBuildings.ToList());
//return View();
}
截至目前它僅返回db.ResidentialBuildings。我也需要它來返回db.ResidentialLocations和其他表。請讓我知道。
UPDATE
下面是該ResidenitialBuilding視圖代碼:
@model BIRDS.ViewModel.BuildingViewModel
@{
ViewBag.Title = "ResidentialBuilding";
}
<div class="row">
<div class="col-md-3">
@Html.DropDownList("ResidentialBuildingType", new SelectList(Model.Select(x => x.type).Distinct()), "Select Building Type")
</div>
<div class="col-md-3">
<select id="ResidentialBuildingStories" name="ResidentialBuildingStories"></select>
</div>
<div class="col-md-3">
<select id="ResidentialBuildingSize" name="ResidentialBuildingSize"></select>
</div>
<div class="col-md-offset-2 col-md-1">
<button type="button" class="btn btn-default btn-round" id="info" data-toggle="tooltip" data-placement="top" title="Click for more info">
<span class="glyphicon glyphicon-info-sign"></span>
</button>
</div>
</div>
<div class="row" id="bldgInfo">
<div class="col-md-2" id="titles">
</div>
<div class="col-md-2" id="values">
</div>
</div>
代碼視圖模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BIRDS.Models;
namespace BIRDS.ViewModel
{
public class BuildingViewModel
{
public List<ResidentialBuilding> ResidentialBuildings { get; set; }
public List<ResidentialLocation> ResidentialLocations { get; set; }
}
}
的BuildingViewModel位於一個名爲視圖模型
好吧,我添加了一行'@model BIRDS.ViewModel.BuildingViewModel;'各方面的意見,但它返回此錯誤: **僅分配,來電,遞增,遞減和新的對象表達式可以用作語句**不完全確定現在發生了什麼。 –
刪除分號 –
並且必須在viewmodel文件夾中添加viewmodel? –