2013-07-15 14 views
0

在我MVC3應用程序,我想填充的是從數據庫來這裏的數據下拉列表正在使用與數據庫的第一種方法的EntityFramework所以請幫我做這個生成與數據庫下拉列表第一

+0

它不是足夠的信息,讓您有所幫助。嘗試閱讀有關DropDownFor的msdn文檔:http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.108).aspx –

+0

您需要提供一些示例代碼。我們希望看到你自己做了什麼,以便我們能夠幫助糾正你的代碼並提供建議。不要指望從這裏完全解決問題。 –

+0

您是在數據庫中創建一個表格,然後在entityframework中調用該表格,然後創建一個控制器。這就是你如何做的方式?請詳細說明,以便我們可以幫助你。 – bot

回答

1

你沒有提供你迄今爲止所做的任何代碼,所以我對你一直在使用的數據一無所知。我會發布一些代碼,你所要做的就是修改它以適應你的場景。

讓我們藉助簡單的貸款申請解決方案。客戶需要申請貸款,他需要提供銀行業務細節。他將不得不從下拉列表中選擇一家銀行。

讓我們從稱爲Bank的域模型開始。這表示您的數據來自您的數據庫表。讓我們打電話給表Banks

public class Bank 
{ 
    public int Id { get; set; } 

    public string Name { get; set; } 
} 

你的表稱爲Banks看起來就像這樣:

Id | int | not null | primary key 
Name | varchar(50) | not null 

Depeneding你你需要做的,我通常有一個調用我的銀行庫帶回的數據服務層的內容。但看到你只需要帶回數據而沒有別的東西,我們可以跳過服務層。

public class BankRepository : RepositoryBase<Bank>, IBankRepository 
{ 
    public IEnumerable<Bank> FindAll() 
    { 
      return DatabaseContext.Banks; 
    } 
} 

你的數據庫環境看起來就像這樣:

public class DatabaseContext : DbContext 
{ 
    public DbSet<Bank> Banks { get; set; } 
} 

這是怎麼了你的數據檢索方法可能看起來像。這可能不是完整的解決方案,但網上有很多樣本。去Google吧。

讓我們轉到Web應用程序。

您的視圖/頁面將與視圖模型一起使用,而不是您的域模型。您使用視圖模型在視圖/頁面上表示您的數據。因此,在您的創建視圖中,您將傳入一個創建應用程序視圖模型以及您的銀行列表。將通過稱爲依賴注入的技術來提供IBankRepository的實例。我爲此使用Autofac

public class ApplicationViewModel 
{ 
    public int BankId { get; set; } 

    public IEnumerable<Bank> Banks { get; set; } 
} 

您的控制器的操作方法將填充視圖模型並將其發送到您的視圖。

public class ApplicationController : Controller 
{ 
    private readonly IBankRepository bankRepository; 

    public ApplicationController(IBankRepository bankRepository) 
    { 
      this.bankRepository = bankRepository; 
    } 

    public ActionResult Create() 
    { 
      ApplicationViewModel viewModel = new ApplicationViewModel 
      { 
       Banks = bankRepository.FindAll() 
      }; 

      return View(viewModel); 
    } 
} 

然後,您的視圖將會收到此視圖模型並執行它需要做的事情。在這種情況下,它會填充你的銀行下拉。

@model YourProject.ViewModels.Applications.ApplicationViewModel 

@using (Html.BeginForm()) 
{ 
    <tr> 
      <td class="edit-label">Bank: <span class="required">**</span></td> 
      <td> 
       @Html.DropDownListFor(
        x => x.BankId, 
        new SelectList(Model.Banks, "Id", "Name", Model.BankId), 
        "-- Select --" 
       ) 
       @Html.ValidationMessageFor(x => x.BankId) 
      </td> 
    </tr> 
} 

我不是什麼你的經驗,但從你的問題來看,你似乎還需要做大量的研究。網上有大量的例子。您需要完成Entity Framework code firstASP.NET MVC的樣品。投入一些時間,你會在以後獲得獎勵。

我希望這個工作,並祝你好運。解決方案可能不是您想要的,但它可以幫助您指引正確的方向。

+0

現在,這是一個全面的答案,乾杯! –

0

讓假設你有像這樣

public class ProductBrand 
    { 
     //Eg. Nokia, Hp, Dell 
     /// <summary> 
     /// Second Level to Category 
     /// Has Foreign Key to category table 
     /// </summary> 
     [Key]  
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ProductBrandId { get; set; } 

     [Display(Name = "Category")] 
     public int ProductCategoryId { get; set; } 
     public virtual ProductCategory ProductCategory { get; set; } 

     [Required(ErrorMessage = "This field is required")] 
     [StringLength(300, ErrorMessage = "This field must not be greater than 300 xters long")] 
     [Display(Name="Name")] 
     public string BrandName { get; set; } 

     public IList<Product> Products { get; set; } 

     [Display(Name = "Status")] 
     public bool ActiveStatus { get; set; } 


    } 

這意味着這種模式有一個外鍵的名字在創建產品品牌觀然後ProductCategoryId你需要包含所有產品分類從選擇下拉列表的類。

只需創建一個ActionResult像這樣

public ActionResult CreateProductBrand() { 

      ViewBag.ProductCategoryId = new SelectList(context.ProductCategories, "ProductCategoryId", "CategoryName"); 
      return View(); 
     } 

然後調用viewbag你corrensponding視圖像這樣:

@using (Html.BeginForm()) { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 


      <table style="width:400px" class="post-form"> 
       <tr> 
        <td>Category</td> 
        <td> 
         <div class="editor-field"> 

          @Html.DropDownList("ProductCategoryId", String.Empty) 
          @Html.ValidationMessageFor(model => model.ProductCategoryId) 
         </div> 
        </td> 
       </tr> 
      </table> 
     <table style="width:400px" class="post-form"> 
       <tr> 
        <td>Brand</td> 
        <td> 
         <div class="editor-field"> 
          @Html.EditorFor(model => model.BrandName) 
          @Html.ValidationMessageFor(model => model.BrandName) 
         </div> 
        </td> 
       </tr> 
       <tr> 
        <td>Status</td> 
        <td> 

         <div class="editor-field"> 
          @Html.EditorFor(model => model.ActiveStatus) 
          @Html.ValidationMessageFor(model => model.ActiveStatus) 
         </div> 
        </td> 
       </tr> 
       <tr> 
        <td></td> 
        <td> 
         <input type="submit" class="button blue" value="Save" /> 
         <a href="@Url.Action("ProductBrand", "ProductSetup")" class="button">Cancel 
         </a> 
        </td> 
       </tr> 
      </table> 


    } 
0

視圖模型:

public List<Item> _Items { get; set; } 
    public int_newID { get; set; } 

型號項目

public int_id { get; set; } 
    public string _name { get; set; } 

控制器: 填充_items的數據和發送列表視圖

查看:

@Html.DropDownListFor(c => c._newID, new SelectList(Model._Items , "_id", "_name"),--Select Item--")