2016-09-01 19 views
0

我使用asp.net mvc與entityframwork。我在模型中添加了一個新項Linq - Sql類,並且我命名爲:StickerPrinter.dbml。該數據庫是一個現有的數據庫從externe數據庫表dropdownlist

我添加了一個表:Verploegen_TEST_Location。

但現在我嘗試添加一個下拉列表。由於Verploegen_TEST_Location有五個位置。

看到圖像 enter image description here

我在MVC現在有這樣的:

public ActionResult Index() 
     { 
      Verploegen_TEST_Location location = new Verploegen_TEST_Location(); 


     } 

,如果我做F12上Verploegen_TEST_Location位置我看到表的所有屬性。但我有點與索引方法掙扎。因爲通常情況下,我使用的DbContext是這樣的:

public class StickerPrinterContext:DbContext 
    { 

     public StickerPrinterContext():base("StickerPrinterContext") 
     {} 

     public DbSet<Printer>printers { get; set; } 
     public DbSet<Vestiging>Vestigingen { get; set; } 
     public DbSet <Vendor>Vendors { get; set; } 
     public DbSet<Verploegen_TEST_Location> Locations { get; set; } 
    } 

但我認爲添加表中stickerPrinterContext現在沒有必要。

因此,建立與LINQ的下拉功能的INdex方法?

謝謝

所以,如果我去Verploegen_TEST_Location我看到這一點:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")] 
    public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged 
    { 

     private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

     private System.Data.Linq.Binary _timestamp; 

     private string _Code; 

     private string _Name; 

     private string _Name_2; 

     private string _Address; 

而且我現在有我的索引控制器是這樣的:

public ActionResult Index() 
     { 
      IList<Verploegen_TEST_Location> locationList = new List<Verploegen_TEST_Location>(); 
      var query = from location in db.Locations 
         select location; 

      return View(locationList); 


     } 

,但如果我運行代碼現在,我收到此錯誤:

發現一個或多個驗證錯誤du環模型生成:

StickerPrinterWeb.DAL.Verploegen_TEST_Location: : EntityType 'Verploegen_TEST_Location' has no key defined. Define the key for this EntityType. 
Locations: EntityType: EntitySet 'Locations' is based on type 'Verploegen_TEST_Location' that has no keys defined 

所以我所做的就是努力在模型中添加一個新的類:

public class Verploegen_TEST_Location 
    { 
    } 

但我會得到一個錯誤:

代碼說明工程線路列在抑制狀態 CS0260對'Verploegen_TEST_Location'類型的聲明缺少部分修飾符;這種類型的其他部分存在申報8 StickerPrinterWeb 18主動

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.42000 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 

我已閱讀本:

To conclude, Linq to Sql dbml file is an auto-generated file that you should never really need to look the code. The Visual Designer is powerful enough to handle all modifications possible with Linq to Sql. 

但如何處理索引方法呢?

你的意思是我必須這樣做?

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")] 
    public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged 
    { 

     private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

     private System.Data.Linq.Binary _timestamp; 

     [Key] 
     private string _Code; 

     private string _Name; 

     private string _Name_2; 

     private string _Address; 

因爲在上面的文件我看到這一點:

#pragma warning disable 1591 
//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.42000 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace StickerPrinterWeb.Models 

enter image description here

,我有一個視圖模型是這樣的:

public class Vm_LocationsList 
    { 
     [Key] 
     public string _Code { get; set; } 
     public string Name { get; set; } 
    } 

我現在有這樣的:

public ActionResult Index() 
     { 
      // declare your view model if you have any 
      var vm_LocationList = new Vm_LocationsList(); 

      var locationList = new List<Verploegen_TEST_Location>(); 
      var query = (from location in db.Locations 
         select location).ToList(); 

      var selectList = new List<SelectListItem>(); 

      foreach (var items in query) 
      { 
       selectList.Add(new SelectListItem { Text = items.Name, Value = items.Code }); 
      } 

      model.SelectList = selectList; 
      return View(model); 
     } 

但模型的刪減在哪裏?

好的,謝謝。但所以我不必編輯

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")] 
    public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged 
    { 

     private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

     private System.Data.Linq.Binary _timestamp; 

     [Key] 
     private string _Code; 

我先做了什麼?

但我仍然得到錯誤:

StickerPrinterWeb.DAL.Verploegen_TEST_Location: : EntityType 'Verploegen_TEST_Location' has no key defined. Define the key for this EntityType. 

對於cleareness。這是我的Verploegen_TEST_Location:它的類部分:

#pragma warning disable 1591 
//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.42000 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace StickerPrinterWeb.Models 
{ 
    using System.Data.Linq; 
    using System.Data.Linq.Mapping; 
    using System.Data; 
    using System.Collections.Generic; 
    using System.Reflection; 
    using System.Linq; 
    using System.Linq.Expressions; 
    using System.ComponentModel; 
    using System; 
    using System.ComponentModel.DataAnnotations; 

    [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="VERPLOEGEN-NAV2009R2-TEST")] 
    public partial class StickerPrinterDataContext : System.Data.Linq.DataContext 
    { 

     private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); 

    #region Extensibility Method Definitions 
    partial void OnCreated(); 
    partial void InsertVerploegen_TEST_Location(Verploegen_TEST_Location instance); 
    partial void UpdateVerploegen_TEST_Location(Verploegen_TEST_Location instance); 
    partial void DeleteVerploegen_TEST_Location(Verploegen_TEST_Location instance); 
    #endregion 

     public StickerPrinterDataContext() : 
       base(global::System.Configuration.ConfigurationManager.ConnectionStrings["VERPLOEGEN_NAV2009R2_TESTConnectionString"].ConnectionString, mappingSource) 
     { 
      OnCreated(); 
     } 

     public StickerPrinterDataContext(string connection) : 
       base(connection, mappingSource) 
     { 
      OnCreated(); 
     } 

     public StickerPrinterDataContext(System.Data.IDbConnection connection) : 
       base(connection, mappingSource) 
     { 
      OnCreated(); 
     } 

     public StickerPrinterDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
       base(connection, mappingSource) 
     { 
      OnCreated(); 
     } 

     public StickerPrinterDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
       base(connection, mappingSource) 
     { 
      OnCreated(); 
     } 

     public System.Data.Linq.Table<Verploegen_TEST_Location> Verploegen_TEST_Locations 
     { 
      get 
      { 
       return this.GetTable<Verploegen_TEST_Location>(); 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")] 
    public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged 
    { 

     private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

     private System.Data.Linq.Binary _timestamp; 

     [Key] 
     private string _Code; 

     private string _Name; 

     private string _Name_2; 

     private string _Address; 

     private string _Address_2; 

     private string _City; 

     private string _Phone_No_; 

     private string _Phone_No__2; 

     private string _Telex_No_; 

     private string _Fax_No_; 

     private string _Contact; 

     private string _Post_Code; 

     private string _County; 

     private string _E_Mail; 

     private string _Home_Page; 

     private string _Country_Region_Code; 

     private byte _Use_As_In_Transit; 

     private byte _Require_Put_away; 

     private byte _Require_Pick; 

     private string _Cross_Dock_Due_Date_Calc_; 

     private byte _Use_Cross_Docking; 

     private byte _Require_Receive; 

     private byte _Require_Shipment; 

     private byte _Bin_Mandatory; 

     private byte _Directed_Put_away_and_Pick; 

     private int _Default_Bin_Selection; 

     private string _Outbound_Whse__Handling_Time; 

     private string _Inbound_Whse__Handling_Time; 

     private string _Put_away_Template_Code; 

     private byte _Use_Put_away_Worksheet; 

     private byte _Pick_According_to_FEFO; 

     private byte _Allow_Breakbulk; 

     private int _Bin_Capacity_Policy; 

     private string _Open_Shop_Floor_Bin_Code; 

     private string _Inbound_Production_Bin_Code; 

     private string _Outbound_Production_Bin_Code; 

     private string _Adjustment_Bin_Code; 

     private byte _Always_Create_Put_away_Line; 

     private byte _Always_Create_Pick_Line; 

     private int _Special_Equipment; 

     private string _Receipt_Bin_Code; 

     private string _Shipment_Bin_Code; 

     private string _Cross_Dock_Bin_Code; 

     private string _Outbound_BOM_Bin_Code; 

     private string _Inbound_BOM_Bin_Code; 

     private string _Base_Calendar_Code; 

     private byte _Use_ADCS; 

     private string _Counter_Nos_; 

     private string _Payment_Method_comb_; 

     private string _Account_No__Cash; 

     private string _Account_No__Pay_Card; 

     private string _Account_No__Cash_Deliver; 

     private string _Account_No__Pay_Card_Deliver; 

     private decimal _X_coord; 

     private decimal _Y_coord; 

     private byte _Autom__Create_Whse__Shipment; 

     private byte _Block_for_Item_Availability; 

     private string _Printer_Name; 
+0

你的web.config文件中是否有一個名爲「StickerPrinterContext」的連接字符串?你有什麼錯誤嗎? – Developer

+0

爲數據庫中的Verploegen_TEST_Location表添加主鍵,更新edmx文件,你將會很開心。您不必添加您自己的'公共類Verploegen_TEST_Location' – Developer

+0

但它是現有的數據庫和現有的表。代碼是表中的主鍵:Verploegen_TEST_Location。 – SavantCode

回答

0

我看着你的第一個錯誤信息:

Locations: EntityType: EntitySet 'Locations' is based on type 'Verploegen_TEST_Location' that has no keys defined

從我的經驗,確保你補充KeyAttribute裏面所有的DBML模型類的主要字段/屬性在DbSet上下文中使用它們(可能需要在DBML設計器文件上手動編輯,或者在基礎數據庫上添加主鍵,然後重新生成DBML結構)。

關於第二錯誤消息:

CS0260: Missing partial modifier on declaration of type 'Verploegen_TEST_Location'; another partial declaration of this type exists

由於模型類被聲明爲局部的,不能簡單地增加另外的相同的模型類,而無需使用其上局部改性劑,但也可以創建單獨的ViewModels與視圖進行交流並在模型類中執行CRUD。

如果您已經在所有DBML表類中設置了主鍵字段/屬性,則可以嘗試使用Html.DropDownListHtml.DropDownListFor將下拉列表內容傳遞到視圖List<SelectListItem>

編輯:我修改了給定的樣本,以適合你的代碼的描述,包括型號&控制器例行這裏:

// Model 
public class Vm_LocationsList 
{ 
    [Key] 
    public string _Code { get; set; } 
    public string Name { get; set; } 

    // add this line to include drop down list items to your view 
    public List<SelectListItem> SelectList { get; set; } 
} 

// Controller 
public ActionResult Index() 
{ 
    // this is your view model declaration 
    var vm_LocationList = new Vm_LocationsList(); 

    var locationList = new List<Verploegen_TEST_Location>(); 
    var query = (from location in db.Locations 
       select location).ToList(); 

    var selectList = new List<SelectListItem>(); 

    foreach (var items in query) 
    { 
     selectList.Add(new SelectListItem { Text = items.Name, Value = items.Code }); 
    } 

    vm_LocationList.SelectList = selectList; 
    return View(vm_LocationList); 
} 

// View (strongly-typed version) 
@Html.DropDownListFor(m => m.Code, Model.SelectList) 

任何建議表示歡迎。

+0

謝謝。我更新了帖子 – SavantCode

+0

您是否看到我的編輯帖子? – SavantCode

+0

編輯我的答案以匹配您的模型聲明。請注意,您需要在您的視圖模型中包含聲明爲「SelectListItem」的屬性,然後將其傳遞到視圖中。 –

相關問題