2015-01-08 31 views
-3

我有一個項目,我需要兩個項目列表項目A,B,C,D & 1,2,3,4等,這個列表將有一個CRUD界面來更改列表隨着時間的推移(通常只添加到列表中)。C#MVC列表項的集合

然後在一個頁面中,我有一個數據條目,最終用戶將在其旁邊看到兩個帶有CheckBox的列表,以便他們可以選擇哪個列表應用於該條目。所以他們可以從第一個列表A,D和第二個2,4中選擇。我需要將此選擇與名稱,註釋,區域等頁面上的其他條目一起存儲。

我的問題是,在MVC和EF中,我有一個DB和DB模型以及兩個列表的CRUD我甚至有我的數據輸入頁面的設計,但點擊保存按鈕時,我不知道如何最好地存儲和保存這些數據。

我不確定我是否完全描述了這一點,但是我需要一個列表來保存每個列表的可能選擇,然後在輸入數據時將條目的選擇保存回數據庫。

我希望它是有道理的,如果我有代碼,我會發布,但我仍然處於設計階段,所以我所擁有的就是廢話,因此使用了大刪除鍵,我開始全新的尋找想法。

謝謝

懸崖。

[編輯]

我想什麼,我想說的是我想在頁面上兩個多選擇列表和的選擇由這兩種存儲。但是列表中的值也來自DB。

[編輯]

好了,所以看起來像我發現一個插件,將處理列表,實際上是最適合我的需求

MvcCheckBoxList

只需要工作如何有兩個這些清單在單一看法上應該不會太難。

{編輯}

好了這方面的工作,距離有以下爲我的模型......

public class CallResult 
{ 
    [Key] 
    public int CallResultId { get; set; } 
    public string Area { get; set; } 
    public string Number { get; set; } 
    public DateTime Date { get; set; } 
    public ICollection<TargetCustomer> TargetCustomers { get; set; } 

} 

public class TargetCustomer 
{ 
    [Key] 
    public int Id { get; set; } 

    public string Name { get; set; } 
    public virtual ICollection<CallResult> CallResults { get; set; } 
} 

我需要有一個創建頁面與多選擇TargetCustomers,然後用戶選擇的內容作爲CallResult列表的一部分存儲。

我創建行動迄今:

public ActionResult Create() 
    { 
     var targetCustomers = new List<TargetCustomer>() 
     { 
      new TargetCustomer() { Id = 1, Name = "Manger", CallResults = new Collection<CallResult>()}, 
      new TargetCustomer() { Id = 2, Name = "Worker", CallResults = new Collection<CallResult>()}, 
     }; 


     ViewBag.MultiSelectTC = new MultiSelectList(targetCustomers, "Id", "Name"); 
     return View(); 
    } 

視圖中的片段:

<div class="form-group"> 
     <div class="control-label col-md-2"> 
      <label>Target Customer</label> 
     </div> 
     <div class="col-md-10"> 
      @Html.ListBox("targetCustomers", (MultiSelectList)ViewBag.MultiSelectTC) 
     </div> 
    </div> 

現在我只需要找回數據到行動,因此可以存儲對CallResult在EF數據庫和我有這個:

public ActionResult Create([Bind(Include = "CallResultId,Area,Number,Date")] CallResult callResult, int[] targetCustomers) 
    { 
     if (ModelState.IsValid) 
     { 
      // Find Tag from Database 
      // Attach tag entity to Post 

      foreach (var custId in targetCustomers) 
      { 
       var cust = db.TargetCustomer.Find(custId); 
       callResult.TargetCustomers.Add(cust); 
      } 

      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(callResult); 
    } 

但我得到一個Null Ref的例外,當它ge ts到callResult.TargetCustomers.Add(cust);線。

我希望這一切都使得比我漫記昨晚感謝您的幫助更有意義......

回答

1

有取決於數據的一些方法很多。但最終你想要的是一個表關係。 1個用戶可以有多個選項。一個選項可以是多個用戶。

爲了簡單:

Table `option_list_1`: 
id - option_name 
1 - a 
2 - b 

Table `option_list_2`: 
id - option_name 
1 - 1 
2 - 2 

Table `user_data`: 
user_data_id - name - notes 

繼承:

Table `user_options`: 
user_data_id - discriminator - option_id 

非繼承:

Table `user_options_1` 
user_data_id - option_id 

Table `user_options_2` 
user_data_id - option_id