Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:18:12+00:00 2026-06-10T13:18:12+00:00

I’m trying to figure how to properly represent values, process selected and later on

  • 0

I’m trying to figure how to properly represent values, process selected and later on edit form display checked items as checked and other possible values as nonchecked.

I already try something but I’m not sure am I on the right track. With using these solution edit form only display checked item, and not showing other non checked.

If you have better solution please share, cause I’m trying to figure how these work.

Here it is: Assume there is User which may belong to one or many Roles. So I would represent these trough two viewmodels, UserViewModel and 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;}

And inside EditorTemplates I have RoleViewModel.cs

@model Models.RoleViewModel

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

And finally inside Create view I display these like these
@model Models.UserViewModel

<div> User roles </div> <div> @Html.EditorFor(m => m.Roles) </div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:18:13+00:00Added an answer on June 10, 2026 at 1:18 pm

    Here’s how to do it:

    UserViewModel

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

    FormView

    You need to loop on all Roles that exists in your application. Then, create a checkbox that will be linked to your UserViewModel.RolesIdsSelected and compare the values to the current role.RoleId that is printed. If there’s a match, there will be a check.

    @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));
        }
    }
    

    When you will post your data, you will have in the UserViewModel.RolesIdsSelected the ids that have been checked by the user. Then, save the change!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.