2017-03-16 20 views
0

我有關係的許多一對多在我的模型下面的實體之間定義的關係 -添加和使用編輯的信息多到許多在ASP.NET使用下拉列表MVC

疾病>症狀 (一種疾病可以有多種症狀,一種症狀可能屬於多種疾病)

我正在使用基於jQuery的多選下拉菜單(Select2)在創建疾病入口時關聯多個症狀。

到目前爲止,我已經獲得了正確的添加疾病信息。我無法獲得編輯部分。我已經在這裏查看了幾個帖子,但似乎並沒有看到它的正確性,因爲其中一些處理複選框而不是下拉菜單。

這裏是到目前爲止的代碼:

模型

public class Disease 
{ 
    public Disease() 
    { 
     this.Indications = new HashSet<App.Models.Indication.Indication>(); 
    } 

    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public Int32 DiseaseID { get; set; } 

    [Required] 
    [Display(Name = "Disease")] 
    [StringLength(100)] 
    public string DiseaseName { get; set; } 

    public virtual ICollection<App.Models.Indication.Indication> Indications { get; set; }    
} 

public class DiseaseAddViewModel 
{ 
    public Disease Disease {get; set;} 
    public IEnumerable<SelectListItem> Indications { get; set; } 
    public Int32[] SelectedIndications { get; set; }   
} 

從視圖的摘錄如下 -

<div class="col-sm-9"> 
@Html.DropDownListFor(m => m.SelectedIndications, Model.Indications, new { @class="select2_demo_1 form-control", @multiple="multiple" }) 
</div> 

控制器

[HttpGet] 
    public ActionResult Add() 
    { 
     var model = new DiseaseAddViewModel(); 
     var allIndications = db.Indication.ToList(); 
     model.Indications = allIndications.Select(o => new SelectListItem 
      { 
       Text = o.IndicationName, 
       Value = o.IndicationID.ToString() 
      });   
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Add(DiseaseAddViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      if (!db.Disease.Any(d => d.DiseaseName == model.Disease.DiseaseName)) 
      { 
       Int32[] selectedIndications = model.SelectedIndications; 

       var Disease = new Disease { DiseaseName = model.Disease.DiseaseName }; 
       AddIndications(Disease, selectedIndications); 
       db.Disease.Add(Disease); 
       db.SaveChanges(); 
       TempData["SuccessMsg"] = "Disease information added successfully."; 
      } 
      else 
      { 
       TempData["ErrorMsg"] = "Disease name already exists in the database."; 
      } 
      return RedirectToAction("Index", "Diseases"); 
     } 
     return View(); 
    } 

    private void AddIndications(Disease disease, Int32[] assignedIndications) 
    { 
     for (int item = 0; item < assignedIndications.Length; item++) 
     { 
      var indication = new Indication(); 
      indication.IndicationID = assignedIndications[item]; 
      db.Indication.Attach(indication); 
      disease.Indications.Add(indication); 
     } 
    } 

如何對編輯(獲取和發佈)進行編碼,以便相關指示在下拉列表中預填充,發佈後如何管理添加/刪除相關實體?

如果有更好的方法來執行上面的添加功能,請隨時提出建議。

非常感謝!

+0

請注意指徵和症狀是相同的實體。 – Jason

+0

爲了預先填充選定的選項,您只需在將模型傳遞給視圖(例如'model.SelectedIndications = new int [] {2,4}; return View()之前,在GET方法中設置'SelectedIndications'的值模型);'和值2和4的選項將被選中,但不能使用DropDownListFor()與@ multiple =「multiple」 - 它需要是「@ Html.ListBoxFor()' –

+0

而對於詳細解釋爲什麼'DropDownList()'不起作用,請參考[這個答案](http://stackoverflow.com/questions/40725358/why-does-the-dropdownlistfor-lose-the-multiple-selection-after-submit -but-the-li/40732481#40732481) –

回答

0

問題通過改變模型和控制器的代碼如下解決 -

控制器

[HttpGet] 
    public ActionResult Edit(int? id) 
    { 
     if (id == null) 
     { 
      return RedirectToAction("Index"); 
     } 

     var model = new DiseaseEditViewModel 
     { 
      Disease = db.Disease.Include(i => i.Indications).Include(p => p.LaboratoryParameters).First(i => i.DiseaseID == id) 
     }; 

     if (model.Disease == null) 
      return HttpNotFound(); 

     var allIndicationsList = db.Indication.ToList(); 
     model.Indications = allIndicationsList.Select(o => new SelectListItem 
     { 
      Text = o.IndicationName, 
      Value = o.IndicationID.ToString() 
     }); 

     return View(model); 
    } 

模型

public class DiseaseEditViewModel 
     { 
      public Disease Disease {get; set;} 
      public IEnumerable<SelectListItem> Indications { get; set; } 

      private List<int> _selectedIndications; 

      public List<int> SelectedIndications 
      { 
       get 
       { 
        if (_selectedIndications == null) 
        { 
         _selectedIndications = Disease.Indications.Select(m => m.IndicationID).ToList(); 
        } 
        return _selectedIndications; 
       } 
       set { _selectedIndications = value; } 
      }  
} 

最後,改變了DropdownlistFor在視圖中ListBoxFor由@所建議的史蒂芬Muecke。謝謝。

相關問題