2014-02-17 84 views
2

我下面這個例子如何將複選框值綁定到整數列表?

enter link description here

但是,當我提出,它說不能轉換「假」到「廉政」

,我認爲其或真或假,這是獲得通過,而不是實際值

我做錯了什麼?

我的模型是

public IEnumerable<AllocateRequirementViewModel> Requirements { get; set; } 

    public List<int> RequirementIds { get; set; } 

那麼我的剃鬚刀

<div id="RequirementsContainer"> 
      @foreach (var requirement in Model.Requirements) 
      { 
       <div class="row"> 
        <input id="@requirement.Id" type="checkbox" name="RequirementIds" value="@requirement.Id" /> @requirement.Description 
       </div> 
      } 
     </div> 

回答

1

您的模型將需要同時包含檢查的值,以及所有可能的值。

public class TestViewModel 
{ 
    public Guid Id { get; set; } 
    public IDictionary<Guid, String> AllCheckboxOptions { get; set; } 
    public Guid[] CheckedOptions { get; set; } 
} 

我在我的數據庫使用的GUID作爲的PK所以這些都是從數據庫

然後,我創建了一些擴展方法生成HTML

public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, String name, List<SelectListItem> listInfo, 
     IDictionary<String, Object> htmlAttributes, Int32 columns) 
    { 
     if (String.IsNullOrEmpty(name)) 
      throw new ArgumentException("The argument must have a value", "name"); 
     if (listInfo == null) 
      throw new ArgumentNullException("listInfo"); 
     if (!listInfo.Any()) 
      return new MvcHtmlString(String.Empty); 

     var outerSb = new StringBuilder(); 

     for (Int32 i = 0; i < columns; i++) 
     { 
      var listBuilder = new TagBuilder("ul"); 
      if (columns > 1) 
       listBuilder.MergeAttribute("class", "column"); 

      var innerSb = new StringBuilder(); 

      var take = listInfo.Count % columns == 0 
       ? listInfo.Count/columns 
       : (listInfo.Count/columns) + 1; 

      var items = listInfo.Skip(i * take).Take(take); 

      foreach (var info in items) 
      { 
       var inputBuilder = new TagBuilder("input"); 
       if (info.Selected) inputBuilder.MergeAttribute("checked", "checked"); 
       inputBuilder.MergeAttribute("type", "checkbox"); 
       inputBuilder.MergeAttribute("value", info.Value); 
       inputBuilder.MergeAttribute("id", info.Value); 
       inputBuilder.MergeAttribute("name", info.Value); 

       var labelBuilder = new TagBuilder("label"); 
       labelBuilder.MergeAttribute("for", @info.Value); 
       labelBuilder.InnerHtml = info.Text; 

       var listItemWrapper = new TagBuilder("li"); 
       //may have to encode here. 
       listItemWrapper.InnerHtml = inputBuilder.ToString(TagRenderMode.SelfClosing) + labelBuilder.ToString(TagRenderMode.Normal); 

       innerSb.Append(listItemWrapper.ToString(TagRenderMode.Normal)); 
      } 

      listBuilder.InnerHtml = innerSb.ToString(); 
      outerSb.Append(listBuilder.ToString(TagRenderMode.Normal)); 
     } 

     return new MvcHtmlString(outerSb.ToString()); 
    } 

然後在視圖的ID你可以打電話給你的擴展方法

@Html.CheckBoxList("yourtypes", (from o in Model.AllCheckboxOptions 
             select new SelectListItem 
             { 
              Text = o.Value, 
              Selected = Model.CheckedOptions.Contains(o.Key), 
              Value = o.Key.ToString() 
             }).ToList()) 

我用這種技術與b另外MVC3和MVC4成功。

相關問題