我有這個代碼可以工作。顯示使用asp.net MVC2查看兩個查詢的結果
public class HelloWorldController : Controller
{
UAStagingEntities db = new UAStagingEntities();
public ActionResult Index(int? id)
{
var depot = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == id.Value);
return View(depot.ToList());
}
}
我不知道該怎麼做是顯示一個視圖與兩個查詢的結果。我將如何創建視圖以顯示倉庫和地址?以及如何編碼返回語句?
public class HelloWorldController : Controller
{
UAStagingEntities db = new UAStagingEntities();
public ActionResult Index(int? id)
{
var depot = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == id.Value);
var Address = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == id.Value);
return View(depot.ToList());
}
}
編輯*
我加入這個模型
namespace CustomerCareMVC.Models
{
public class CSLA_StagingModel
{
public List<CSLA_DEPOT> depots { get; set; }
public List<CSLA_ADDRESS> addresses { get; set; }
}
}
而在控制器中添加這個方法
public ActionResult ShowAllTables()
{
var model = new CSLA_StagingModel()
{
depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == 10065),
addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == 10065),
};
return View(model);
}
我得到這兩條線之下波浪線
depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == 10065),
addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == 10065),
與此錯誤消息
錯誤1無法隱式轉換類型 'System.Linq.IQueryable' 到 'System.Collections.Generic.List'。存在明確的轉換(您是否缺少演員?)C:\ CustCareMVC \ CustomerCareMVC \ CustomerCareMVC \ Controllers \ HelloWorldController.cs 59 26 CustomerCareMVC
DevDave,我試圖實現這個代碼,我得到一個錯誤。將其添加到工作編輯下的問題。謝謝 – 2010-10-26 18:07:29
Joshua,我的代碼中有一個錯誤。請注意,該模型現在爲每個變量而不是列表引用IQueryable。這應該可以解決你的問題。 – DevDave 2010-10-26 22:11:40