![](/img/trans.png)
[英]Filtering data from a dropdown list selection in ASP.NET MVC 5, C# and Entity Framework
[英]Select List returns System.Data.Entity.DynamixProxies instead of values in MVC asp.net C#
我有兩張不同的桌子。 用戶和項目詳細信息。 還有兩個不同的控制器可以對這些表進行CRUD操作。 現在,我有一個案例,在User CREATE操作中,我必須從ProjectDetails中的項目列表中選擇項目。 我嘗試了以下方法:
在用戶模型中,我創建了這一行:
public IEnumerable<ProjectDetail> ProjectDetail { get; set; }
在控制器中,在create Action中,我添加了以下代碼:
public ActionResult Create()
{
var model = new UserDetail
{
ProjectDetail = db1.ProjectDetails
};
return View(model);
}
在創建視圖中,我試圖獲取項目ID列表,如下所示:
@Html.DropDownListFor( x => x.ProjectDetail, new SelectList(Model.ProjectDetail, "Project ID"))
但是,我運行,我得到的行數(等於項目數)
System.Data.Entity.DynamicProxies.ProjectDetails_F ###########(有些數字)..
請有人幫幫我嗎?
問候,哈里
[編輯] - 我檢查了調試模式,發現以下..嘗試附加圖像..
我鑽了那個Proxy的東西,在那里找到了ProjectID。 我怎么能得到它?
您正在使用錯誤的重載,請改用:
@Html.DropDownListFor( x => x.ProjectDetail,
new SelectList(Model.ProjectDetail, "ProjectId","ProjectName"))
// where ProjectId is the unique identifier field of `ProjectDetail`
// and `ProjectName` is the text you want to show in the dropdown
在您的代碼中,您沒有告訴html幫助器使用哪些屬性來為datavalue
和datatext
。 您使用的重載是告訴htmlhelper選擇哪個值的重載。
你可以做點什么
var projection = db1.ProjectDetails.Select(t => new ProjectDetailsViewModel
{
Prop1 = t.Prop1,
Prop2 = t.Prop2
});
你能試一下嗎
public ActionResult Create()
{
var model = new UserDetail
{
ProjectDetail = db1.ProjectDetails.ToList()
};
return View(model);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.