2013-05-07 25 views
0

我正在開發使用C#和SQL Server 2005的ASP .Net MVC 3應用程序。
我也使用實體框架和代碼優先方法。
我已經創建了一個'DropDownList'來從我的基表中加載數據。
但因此,此DropDownList告訴我:'NameofApplication.Models.NameofTable'
我不知道爲什麼?
這是DROPDOWNLIST的視圖中的聲明:從DropDownList的基礎加載數據的錯誤ASP MVC 3和EF

<%:Html.Label("Gamme :")%> 
<%: Html.DropDownList("ID_Gamme", String.Empty) %> 

這是Profile_Ga的控制器:

namespace MvcApplication2.Controllers 
{ 
    public class ProfileGaController : Controller 
    { 
     private GammeContext db = new GammeContext(); 

     // 
     // GET: /ProfileGa/ 

     public ViewResult Index(Profile_Ga profile_ga) 
     { 
      ViewBag.ID_Gamme = new SelectList(db.Profil_Gas, "ID_Gamme", profile_ga.ID_Gamme); 
      return View(db.Profil_Gas.ToList()); 


     } 

和Finaly這是模型:

namespace MvcApplication2.Models 
{ 
    public class Profile_Ga 
    { 
     [Required] 
     [Key] 
     [Display(Name = "ID Gamme:")] 
     public string ID_Gamme { get; set; } 

     [Required] 
     [Display(Name = "Gamme Entrante:")] 
     public string In_Ga { get; set; } 

     [Required] 
     [Display(Name = "Gamme Sortante:")] 
     public string Out_Ga { get; set; } 

     [Required] 
     [Display(Name = "Gamme Suivante:")] 
     public string Next_Gamme { get; set; } 

     [Required] 
     [Display(Name = "Etat:")] 
     public string Etat { get; set; } 
     } 
} 

PS :
表中的表名是Profile_Ga

+0

隨着你最近的評論下面,那麼我的第一個答案是正確的。請嘗試並做到這一點。所以你應該在你的控制器方法中使用'返回View(db.Profil_Gas.ToList());'和<%:Html.DropDownList(「ID_Gamme」,new SelectList(Model,「ID_Gamme」,「ID_Gamme」))% >在你看來。請記住,您的模型是'Profile_Ga'的集合。 – 2013-05-07 09:56:55

+0

非常感謝!這是完美的作品! – anouar 2013-05-07 10:07:04

+0

很酷,最後,歡迎您:) – 2013-05-07 10:13:16

回答

1

您在返回的Profile_Ga集合,我覺得你的意思是使用

<%: Html.DropDownList("ID_Gamme", 
    new SelectList(Model, "ID_Gamme", "ID_Gamme ")) %> 

雖然看起來不正確的。在你的方法,你這樣做

public ViewResult Index(Profile_Ga profile_ga) 
{ 
    ViewBag.ID_Gamme = new SelectList(
     db.Profil_Gas, "ID_Gamme", profile_ga.ID_Gamme); 
    // did you plan to return a single instance here 
    // and use that as your model 
    return View(single_instance_of_Profile_Ga); 
} 

那麼你的看法是這樣的

// then you can declare the dropdown like this 
<%: Html.DropDownListFor(m => m.ID_Gamme, (SelectList)ViewBag.ID_Gamme) %> 

UPDATE(從評論)

THX,「你要顯示一個列表,你從你的數據庫到 你的看法「 - >是這個我想要什麼

public ViewResult Index(Profile_Ga profile_ga) 
{ 
    // you need to decide if you want to use this 
    // ViewBag.ID_Gamme = new SelectList(
    //  db.Profil_Gas, "ID_Gamme", profile_ga.ID_Gamme); 
    // or this 
    // return View(db.Profil_Gas.ToList()); 
    // but since it seems you are confuse to what you are doing 
    // then I suggest you just do this 
    // this will return a collection model to your view 
    return View(db.Profil_Gas.ToList()); 
} 

那麼在你看來,你需要做到這一點

<%: Html.DropDownList("ID_Gamme", SelectList(Model) %> 

你打算與下拉做 - >我想,當我選擇一個 項目,,,會出現一個彈出。數據的加載始終是假的,,,爲什麼我 將編輯

你需要修改你的問題,因爲你沒有問,最初。所以我的問題是彈出窗口會有什麼,可能是編輯窗體?你需要提出一個新問題,因爲你必須解釋它,我必須回答,你必須在評論中提問,這將是一個長時間的聊天 - 所以不會那樣。無論如何它會有一個新的問題,並提出一個問題是免費的;)

+0

謝謝,也,,,我試試這個,但聲明在紅色下劃線,錯誤是: System.Web.Mvc.HtmlHelper >'沒有名爲'DropDownListFor'的適用方法,但似乎有按該名稱的擴展方法。擴展方法不能動態分派。考慮轉換動態參數或調用擴展方法而不使用擴展方法語法。 – anouar 2013-05-07 08:46:05

+0

啊對,這是使用Viewbags的缺點。你需要把它轉換成一個'SelectList',看看我更新的答案。 – 2013-05-07 08:50:30

+0

您的解決方案是正確的,但現在出現其他錯誤笑, ID_Gamme以紅色下劃線和錯誤是: 錯誤「System.Collections.Generic.IEnumerable 」不包含定義對於'ID_Gamme'並且沒有找到接受'System.Collections.Generic.IEnumerable '類型的第一個參數的擴展方法'ID_Gamme''可以找到(你是否缺少使用指令或程序集引用?) – anouar 2013-05-07 09:02:16