MvcCheckBoxList icon indicating copy to clipboard operation
MvcCheckBoxList copied to clipboard

Overload function choice problem

Open lansman opened this issue 8 years ago • 1 comments

I have two simple classes:

public class UserVM
{
       public IList<UserRoleVM> PreselectedUserRoles { get; set; }
       public IList<UserRoleVM> AllUserRoles { get; set; }
}

public class UserRoleVM
{
    public int Id {get; set;}
    public string Name {get; set;}
    public bool IsSelected {get; set;}
}

Year, i implement two ways to pass preselected items, but only because none of them works.

After that i calls:

@Html.CheckBoxListFor(user => user.PostedUserRoles.UserRolesIds,
                                  user => user.AllUserRoles,
                                  x => x.Id,
                                  x => x.Name,
                                  ur => ur.IsSelected, // error here
                                  new { @class = "user-roles" })

And got error

CS1061: 'UserVM' does not contain a definition for 'IsSelected' and no extension method 'IsSelected' accepting a first argument of type 'UserVM' could be found (are you missing a using directive or an assembly reference?)

OK, i try:

@Html.CheckBoxListFor(user => user.PostedUserRoles.UserRolesIds,
                                  user => user.AllUserRoles,
                                  x => x.Id,
                                  x => x.Name,
                                  user => user.PreselectedUserRoles, // error here
                                  new { @class = "user-roles" })

CS1061: 'UserRoleVM' does not contain a definition for 'PreselectedUserRoles' and no extension method 'PreselectedUserRoles' accepting a first argument of type 'UserRoleVM' could be found (are you missing a using directive or an assembly reference?)

But when i get rid of 6th param width custom HTML code - all works fine.

// OK
@Html.CheckBoxListFor(user => user.PostedUserRoles.UserRolesIds,
                                  user => user.AllUserRoles,
                                  x => x.Id,
                                  x => x.Name,
                                  user => user.PreselectedUserRoles
// OK
@Html.CheckBoxListFor(user => user.PostedUserRoles.UserRolesIds,
                                  user => user.AllUserRoles,
                                  x => x.Id,
                                  x => x.Name,
                                  ur => ur.IsSelected)

How can i use custom html tags with MvcCheckBoxList?

lansman avatar Sep 09 '16 06:09 lansman

Sorry for the late reply, I'll post it here in case someone else has the same issue.

Unfortunately, those overload parameters have a different meaning depending on number parameters used. I believe custom html tags would only work using full example:

var htmlListInfo = new HtmlListInfo(HtmlTag.vertical_columns, 2, new { @class="styled_list" };
@Html.CheckBoxListFor(x => x.PostedCities.CityIDs,
                      x => x.AvailableCities,
                      x => x.Id,
                      x => x.Name,
                      x => x.SelectedCities,
                      new { @class="styled_checkbox" },      // additional html attributes (for each checkbox and label)
                      htmlListInfo),                         // formatting
                      new[] {"3", "5", "7"},                 // disabled values
                      x => x.Tags)                           // html tags from database

mikhail-tsennykh avatar Sep 29 '16 22:09 mikhail-tsennykh