[英]New to MVC, Populating a DropDownList from a database
我正在嘗試創建一個系統,在該系統中,在填寫網絡表單以向數據庫添加條目的同時,從另一個可編輯數據庫填充了下拉列表。 因此,管理員可以說,在工資單上的雇員列表中添加另一個名稱,然后在下次訪問該名稱時將其填充到下拉列表中。
我嘗試遵循很多其他答案來回答諸如SO之類的問題,但是我什么也做不了。 這是我認為與我最接近的一個: MVC 4數據庫模型中的簡單填充DropDown
使用它,我創建了一個“管道”對象(正在處理的項目的名稱):
[Table("Entries")]
public class pipeline
{
[Key]
public int entryID { get; set; }
public salesStage currentSalesStage { get; set; }
public string Name { get; set; }
}
一個“ salesStage”對象,(這是我希望管理員能夠更改的對象,因此從理論上講,他們可以添加或刪除銷售階段,而員工在向“管道”創建新條目時會看到這一點):
[Table("salesStages")]
public class salesStage
{
[Key]
public int salesStageID { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
}
然后,我得到了ViewModel,該鏈接的答案概述如下:
public class MyViewModel
{
public pipeline Entry { get; set; }
public IEnumerable<SelectListItem> salesStages { get; set; }
}
數據庫上下文:
public class PipelineDB : DbContext
{
public DbSet<pipeline> Entries { get; set; }
}
鏈接答案中概述了ActionResult:
public ActionResult Action(int id)
{
var model = new MyViewModel();
using (var db = new PipelineDB())
{
model.salesStages = db.Entries.ToList().Select(x => new SelectListItem
{
Value = x.currentSalesStage.ToString(),
Text = x.Name
});
}
return View(model);
}
最后,我嘗試將其集成到“創建”視圖中:@Model Pipeline9.Models.MyViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>pipeline</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.currentSalesStage, new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(x => model.currentSalesStage.salesStageID, Model.salesStages)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我認為create視圖是我出問題的地方,因為它給我@ Html.DropDownListFor一個錯誤。 錯誤是:
CS1973:'System.Web.Mvc.HtmlHelper'沒有名為'DropDownListFor'的適用方法,但似乎具有該名稱的擴展方法。 擴展方法不能動態調度。 考慮強制轉換動態參數或在不使用擴展方法語法的情況下調用擴展方法。
編輯:到目前為止一切工作到一定程度,但編譯器一直告訴我@ Html.DropDownListFor(x => model.currentSalesStage.salesStageID,...的“當前上下文中不存在模型”,...
也許試試這個?
<div class="col-md-10">
@Html.DropDownListFor(x => model.currentSalesStage.salesStageID, (SelectList)Model.salesStages)
</div>
當您執行此操作時: model.salesStages = db.Entries.ToList().Select(x => new SelectListItem { Value = x.currentSalesStage.ToString(), Text = x.Name });
您正在將model.salesStages設置為可枚舉的表達式。 如果在選擇之后調用ToList()
,則應將Select(...)
枚舉到具體對象List
中。
碼:
model.salesStages = db.Entries.ToList().Select(x => new SelectListItem
{
Value = x.currentSalesStage.ToString(),
Text = x.Name
}).ToList();
嘗試以下方法:
@Html.DropDownListFor(x => model.currentSalesStage.salesStageID,
new SelectList(Model.salesStages, "Value", "Text"))
編輯
@ nwash57根據您最近的評論,您的currentSalesStage模型為null,因此為了解決該問題,請將pipeline
類的新實例分配到控制器的此屬性中,並將其傳遞給視圖,例如
public ActionResult Action(int id)
{
var model = new MyViewModel();
using (var db = new PipelineDB())
{
model.salesStages = db.Entries.ToList().Select(x => new SelectListItem
{
Value = x.currentSalesStage.ToString(),
Text = x.Name
});
model = new pipeline();
}
return View(model);
}
這樣可以解決您的問題。
終於想通了。 使用ViewBag填充下拉列表。
首先將所需的數據庫添加到控制器中:
private MyDataBase db = new MyDataBase();
然后在控制器中將ViewBag設置為新的SelectList:
ViewBag.MyViewBagVariable = new SelectList(db.MyDataBaseModels, "ID", "ID");
最后,在視圖下,將DropDownListFor幫助器與ViewBag一起使用以填充下拉列表:
@Html.DropDownListFor(model => model.MyDataBaseValue, (SelectList)ViewBag.MyViewBagVariable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.