我已經創建了一個使用實體框架代碼的MVC項目。我的模型只是一種收集信息的形式。MVC下拉列表與實體框架
public class Application
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
public int SSN { get; set; }
public DateTime DOB { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State {get; set; }
public string Zip { get; set; }
public int HomePhone { get; set; }
public int BusinessPhone { get; set; }
public int MobilePhone { get; set; }
}
我的目標是創建一個下拉與所有的國家的名單,但我發現這個因爲我已經通過腳手架,可欣賞創建的數據庫是非常困難和控制器。有沒有簡單的方法來實現這一點,並將其與現有數據庫綁定?我已經搜索了幾乎整整一天,沒有運氣。對模型/控制器/視圖包含什麼內容的概述/解釋將非常棒!
更新:我已經創建了一個名爲「國家」屬性「ID」和「Statename的」新模式,並在數據庫中創建一些狀態。在創建操作方法在我的 「應用程序」 控制我有:
控制器
public ActionResult Create()
{
ApplicationDbContext db = new ApplicationDbContext();
this.ViewData["Id"] = new SelectList(db.States.ToList(), "Id", "StateName");
return View();
}
查看
@Html.DropDownList("Id")
現在的問題是,我得到這個錯誤「沒有類型爲」IEnumerable「的ViewData項目具有」Id「鍵。」非常感謝您的幫助!
什麼問題,如何從數據庫中提取數據,或顯示在用戶界面? – 2014-10-30 20:55:08
嗯,我不確定如何去做。我目前有一個應用程序表(上面)。我需要在設計器中手動創建另一個名爲「States」的表嗎?之後,我需要知道如何提取數據,然後將其顯示在我現有的表單上。 – dc922 2014-10-30 20:59:29
查看一些教程http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an- asp-net-mvc-application – 2014-10-30 21:02:48