2016-08-11 19 views
1

我需要填充表單上設備的複選框列表以供用戶請求設備。列表的數據存儲在名爲「設備」的表格中。我正在使用EF 6數據庫。該視圖是強類型的,並將寫入「訂單」表。我被困在如何使用視圖模型而不是ViewBag來填充表單的複選框列表。我曾看過MikesDotNetting,Rachel Lappel關於視圖模型和其他幾個人的帖子,這對我來說沒有意義。下面
代碼:.NET MVC 5使用視圖模型從數據庫複選框列表

public class Equipment 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
    public string Method { get; set; } 
    public bool Checked { get; set; } 
} 

public class Order 
{ 
public int id{ get; set; }  
public string Contact_Name { get; set; } 
public List<Equipment>Equipments { get; set; } 
public string Notes { get; set; } 

} 

控制器:

 [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "Contact_Name,Equipment,Notes")] Order order) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Orders.Add(order); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(order); 
    } 

查看

@model CheckBoxList.Models.Order 

    @{ 
    ViewBag.Title = "Create"; 
    } 

    <h2>Create</h2> 


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

    <div class="form-horizontal"> 
     <h4>Order</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
<div class="form-group"> 
      @Html.LabelFor(model => model.Contact_Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Contact_Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Contact_Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
<div class="form-group"> 
      //checkbox list populated here 
     </div> 


<div class="form-group"> 
      @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

回答

0

做一個在您的列表循環,併爲它的每個項目的複選框。

<div class="form-group"> 
    @for (int i = 0; i < Model.Equipments.Count(); i++) 
    {  
     @Html.CheckBoxFor(x => x.Equipments[i].Checked) 
     @Model.Equipments[i].Description 

     //If you need to hide any values and get them in your post 
     @Html.HiddenFor(x => x.Equipments[i].Id) 
     @Html.HiddenFor(x => x.Equipments[i].Method) 
    } 
</div> 
+0

我似乎沒有正確填充Model.Equipments,因爲我收到一個NullReferenceException錯誤。我更新了上面的原始代碼。設備列表現在應該是調用設備類而不是查看模型 –

0

看到這個答案怎麼辦呢How to bind checkbox values to a list of ints?

這個例子使用的GUID作爲PK的,但可以用INT的容易更換。

我打算假設你的設備類是你的EF實體。

所以,你正在創建您的訂單頁面,以便讓我們先從CreateOrderViewModel

public class CreateOrderViewModel 
{ 
public string Contact_Name { get; set; } 
public Dictionary<int, string> AllEquipment{ get; set; } 
public int[] SelectedEquipment { get;set; } 
public string Notes { get; set; } 
} 

填充AllEquipment只有ID和一件裝備的名字。這是使用設備ID的值顯示所有設備複選框所需的設備的完整列表。

喜歡的東西

var viewModel = new CreateOrderViewModel { 
AllEquipment = context.Equipment.ToDictionary(e => e.Id, e.Description); 
} 

SelectedEquipment是設備的使用複選框選中列表中。因此,當您發佈此信息時,SelectedEquipment屬性將包含需要附加到訂單的所有ID的列表。

當您創建訂單時,只需遍歷列表並將它們添加到訂單實體的設備列表中即可。

+0

設備和訂單都是EF實體。我沒有連接如何填充Dictionary <> for AllEquipment。 –

+0

更新瞭如何填充字典的答案 – Fran

+0

字典(context.Equipment.ToDictionary(e => e.Id,e.Description);)填充在VM中嗎? –

相關問題