2012-08-23 103 views
2

我想知道如何正確表示值,選擇的過程以及稍後在編輯窗體中顯示選中的項目作爲選中項以及其他可能的值作爲未選中項。mvc中的複選框值

我已經嘗試了一些東西,但我不確定我是否在正確的軌道上。使用這些解決方案編輯表單只顯示選中的項目,而不顯示其他未選中的項目。

如果你有更好的解決方案,請分享,因爲我試圖找出這些工作。

這裏是:假設有用戶可能屬於一個或多個角色。所以,我將代表這些槽中雙的ViewModels,UserViewModel和RoleViewModel

**UserViewModel.cs** 

public int id {get; set;} 
public string username {get; set;} 

public UserViewModel() 
{ 
    Roles = new List<RoleViewModel>(); 
} 

public void SendToDomain(User user, ISession nhibSession) 
{ 
    if(user.Roles != null) 
    { 
     user.Roles.Clear();//Clear previous selected items 
     foreach(Role role in Roles) 
     { 
     if(role.IsInRole) 
      user.Role.Add(nhibSession.Load<Role>(user.RoleId)); 
     } 
    } 
} 

**RoleViewModel** 
public bool IsInRole {get; set;} 

[HiddenInput(displayValue = false)] 
public int RoleId {get; set;} 

[HiddenInput(displayValue = true)] 
public string RoleName {get; set;} 

而且裏面EditorTemplates我有RoleViewModel.cs

@model Models.RoleViewModel 

@Html.CheckBoxFor(m=>m.IsInRole) 
@Html.HiddenFor(m=>m.RoleId) 
@Html.LabelFor(m=>m.IsInRole, Model.RoleName) 

最後內創建視圖我展示這些喜歡這些 @model Models.UserViewModel

<div> User roles </div> <div> @Html.EditorFor(m => m.Roles) </div> 

回答

0

這裏是如何做到這一點:

UserViewModel

public int id {get; set;} 
public string username {get; set;} 
public int[] RoleIdsSelected { get; set; } 

FormView控件

您需要循環存在於應用程序的所有角色。然後,創建一個複選框,該複選框將鏈接到您的UserViewModel.RolesIdsSelected,並將這些值與當前角色進行比較。如果有一場比賽,將會有一張支票。

@model UserViewModel 

@{ 
    foreach (var role in (IEnumerable<Role>)ViewBag.AllRoles) 
    { 
     <label> 
      @Html.CheckBoxFor(x => x.RoleIdsSelected, role.RoleId) 
      @role.RoleName 
     </label> 
    } 
} 

CheckboxExtensions

public static class InputExtensions 
{ 
    public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value) 
    { 
     return htmlHelper.CheckBoxFor<TModel, TProperty>(expression, value, null); 
    } 

    public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes) 
    { 
     return htmlHelper.CheckBoxFor<TModel, TProperty>(expression, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); 
    } 

    public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, IDictionary<string, object> htmlAttributes) 
    { 
     ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData); 
     return CheckBoxHelper<TModel, TProperty>(htmlHelper, modelMetadata, modelMetadata.Model, ExpressionHelper.GetExpressionText(expression), value, htmlAttributes); 
    } 

    private static MvcHtmlString CheckBoxHelper<TModel, TProperty>(HtmlHelper htmlHelper, ModelMetadata metadata, object model, string name, object value, IDictionary<string, object> htmlAttributes) 
    { 
     if (value == null) 
     { 
      throw new ArgumentNullException("value"); 
     } 

     RouteValueDictionary routeValueDictionary = htmlAttributes != null ? new RouteValueDictionary(htmlAttributes) : new RouteValueDictionary(); 

     string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); 

     if (string.IsNullOrEmpty(fullHtmlFieldName)) 
     { 
      throw new ArgumentException("Le nom du control doit être fourni", "name"); 
     } 

     TagBuilder tagBuilder = new TagBuilder("input"); 

     tagBuilder.MergeAttributes<string, object>(htmlAttributes); 
     tagBuilder.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.CheckBox)); 
     tagBuilder.MergeAttribute("name", fullHtmlFieldName, true); 

     string text = Convert.ToString(value, CultureInfo.CurrentCulture); 

     tagBuilder.MergeAttribute("value", text); 

     if (metadata.Model != null) 
     { 
      foreach (var item in metadata.Model as System.Collections.IEnumerable) 
      { 
       if (Convert.ToString(item, CultureInfo.CurrentCulture).Equals(text)) 
       { 
        tagBuilder.Attributes.Add("checked", "checked"); 
        break; 
       } 
      } 
     } 

     return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing)); 
    } 
} 

當你將發佈你的數據,你將不得不在UserViewModel.RolesIdsSelected已經由用戶選擇了IDS。然後,保存更改!