2014-07-02 46 views
-1

我有一個問題,我需要將多個表返回到視圖。截至目前,我的索引操作結果只能獲得一個表,但這需要修改。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位於一個名爲視圖模型

回答

3

文件夾中你需要t在控制器

public Class BuildingViewModel 
{ 

public List<ResidentialBuilding> ResidentialBuildings {get;set;} 

public List<ResidentialLocation> ResidentialLocations {get;set;} 

} 

現在::○創建一個視圖模型

public class ResidentialBuildingController : Controller 
{ 
    private BIRDSResidentialEntities db = new BIRDSResidentialEntities(); 

    // GET: ResidentialBuilding 
    public ActionResult Index() 
    { 
     BuildingViewModel model = new BuildingViewModel(); 
     model.ResidentialBuildings = db.ResidentialBuildings.ToList(); 
     model.ResidentialLocations = db.ResidentialLocations.ToList(); 

     return View(model); 
    } 
} 

,並強烈反對這種觀點模型輸入您的觀點:

@model YourNameSpace.ViewModels.BuildingViewModel 

欲瞭解更多詳細信息,您可以參考以下鏈接:

http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx

http://www.dotnet-tricks.com/Tutorial/mvc/QHQT270712-Understanding-ViewModel-in-ASP.NET-MVC.html

http://sampathloku.blogspot.com/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

http://www.edandersen.com/2013/05/30/asp-net-mvc-basics-part-2-viewmodel-to-model-mapping-and-editing/

+0

好吧,我添加了一行'@model BIRDS.ViewModel.BuildingViewModel;'各方面的意見,但它返回此錯誤: **僅分配,來電,遞增,遞減和新的對象表達式可以用作語句**不完全確定現在發生了什麼。 –

+0

刪除分號 –

+0

並且必須在viewmodel文件夾中添加viewmodel? –