Asked by:
Pass List of Checkboxes into View

Question
-
User490317677 posted
I have a list of items that will be associated to a user. It's a one-to-many relationship. I want the entire list of items passed into the view so that they can choose from ones that are not associated to them yet (and also see those that are already associated). I want to create checkboxes from these. I then want to send the selected ones back into the controller to be associated. How can I pass in the list of all of them, including those that aren't yet associated, and reliably pass them back in to be associated?
ViewModel:
public class RightsVM { public int ID { get; set; } public string Name { get; set; } public bool IsSelected { get; set; } } public class UserVM { public UserVM() { Rights = new List<RightsVM>(); } public int ID { get; set; } public string Name { get; set; } public List<RightsVM> Rights{ get; set; } }
In the GET method, i get Problem/Error when i try to assigned property for Roles:
Cannot Convert string to Generic List:
Given
rights.RightsCode (Is property which is coming from Database)
is a string, how can i expecting that string become aList<RightsVM>
public ActionResult UserEdit(int IDs) { UserVM model = new UserVM(); var query = (from px in db.PX2 join rights in db.Rights on px.Email_ID equals rights.Mail where px.Id == IDs select new UserVM { Rights = rights.RightsCode, **//Cannot Convert String to List** ID = rights.Id, }); return View(model); }
View:
@using (Html.BeginForm()) { @Html.HiddenFor(m => m.ID) @Html.DisplayFor(m => m.Name) for (int i = 0; i < Model.Roles.Count; i++) { @Html.HiddenFor(m => m.Roles[i].ID) @Html.CheckBoxFor(m => m.Roles[i].IsSelected) @Html.LabelFor(m => m.Roles[i].IsSelected, Model.Roles[i].Name) } <input type"submit" /> }
Tuesday, August 27, 2019 1:48 PM
All replies
-
User475983607 posted
The MVC Getting Started tutorials covers this specific pattern; https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
I recommend starting for the begging of the tutorial though.
Tuesday, August 27, 2019 2:31 PM -
User-474980206 posted
this query:
var query = (from px in db.PX2 join rights in db.Rights on px.Email_ID equals rights.Mail where px.Id == IDs select new UserVM { Rights = rights.RightsCode, **//Cannot Convert String to List** ID = rights.Id, });
returns a row for every Rights row. you use the group by to group the rows by Id.
var query = (from px in db.PX2 join rights in db.Rights on px.Email_ID equals rights.Mail where px.Id == IDs group new {px, rights} by new {px.Id} into g select new UserVM { Rights = g.Rights.Select(r => new RightsVM { r.Id, r.Name, r.IsSelected }).ToList() ID = g.px.Id, }).ToList();
Tuesday, August 27, 2019 4:04 PM -
User490317677 posted
No Luck Bruce :( get serveral errors.
Wednesday, August 28, 2019 8:59 AM -
User-2054057000 posted
Your question is based entirely on the Model Binding concept. I found that a similar question has been answered here, please check it and let me know if it is useful.
Wednesday, August 28, 2019 9:30 AM -
User-17257777 posted
Hi ManDown,
The type of “Rights” in UserVM is List<RightsVM> and the type of “RightsCode” is String, String cannot convert to List, so you have this error. Assuming that you store the rights in the RightsCode and are separated by "," you can first convert the RightsCode to an array using the split(',') method like this:
string RightsCode = "r1,r2,r3,r4,r5"; string[] RightsArray= RightsCode.Split(',');
Then convert the resulting array into a List<RightsVM>. If the actual situation is different from what I guess, I hope that you can provide us with the specific code of the “PX2” and “Rights” entities.
Best Regards,
Jiadong Meng.
Thursday, August 29, 2019 3:26 AM