I am working with a problem. I can currently display data on a page with the NonFactors Grid MVC6, though it is incomplete data. Currently for Boolean values it is displaying a True or False on the page. I am having trouble trying to get it to display a checkbox. I have tried Html.CheckboxFor and <input type="checkbox" /> with no luck. So here is what I am working with. I have four viewmodels for the page, and a partial view that works in conjunction with the view. The page is only for editing, but the only editing that needs to take place is with the Boolean values and a selectlist for each row of the grid. What I am looking for is advice with the partial view so that I can get the checkbox and selectlist to display.
Here is my viewmodels in question:
public class CharRepViewModel { public CharRepViewModel() { this.Name = new CharFullNameViewModel(); this.Reputation = new List<CharRepListViewModel>(); } public CharFullNameViewModel Name { get; set; } public List<CharRepListViewModel> Reputation { get; set; } } public class CharFullNameViewModel { public int Char_ID { get; set; } public string CharFullName { get; set; } } public class CharRepListViewModel { public CharRepListViewModel() { this.CharID = CharID; this.FactionID = FactionID; this.FactionName = FactionName; this.FactionSet = FactionSet; this.RepLevelID = RepLevelID; this.RepLevels = new List<RepLevelListViewModel>(); } public int CharID { get; set; } public int FactionID { get; set; } public string FactionName { get; set; } public bool FactionSet { get; set; } public int RepLevelID { get; set; } public List<RepLevelListViewModel> RepLevels { get; set; } } public class RepLevelListViewModel { public int FactionID { get; set; } public int RepLevelID { get; set; } public string RepLevelName { get; set; } public bool RepIsChecked { get; set; } }
The RepLevelListViewModel is what I want for the SelectList in the grid. The FactionSet in the CharRepListViewModel is what I want for a checkbox. This will hopefully allow the multiselect dropdown to be accessible if checked, and not accessible if uncheck. Of which I have not figured out how to do yet either. The CharRepViewModel brings everything together for the main view. And yes my RepLevelID in the CharRepListViewModel is currently unused, I added it there, but have not been able to make the data use it correctly yet, but I have left it there currently while building the view in case I need it.
Here is my controller:
public async Task<IActionResult> Edit(int? id) { if (id == null) { return BadRequest(); } var factIdList = await _db.Database.SqlQuery<CharRepListViewModel>(sql: "GetCharacterReputations @p0", parameters: new object[] { id }).ToListAsync(); var eReps = await _db.Database.SqlQuery<RepLevelListViewModel>(sql: "GetCharacterReputationsChecks @p0", parameters: new object[] { id }).ToListAsync(); foreach (CharRepListViewModel cr in factIdList) { cr.RepLevels.AddRange(eReps.Where(a => a.FactionID == cr.FactionID)); } var characterReputation = new CharRepViewModel() { Name = await _db.Database.SqlQuery<CharFullNameViewModel>(sql: "GetCharacterFullName @p0", parameters: new object[] { id }).FirstOrDefaultAsync(), Reputation = factIdList }; if (characterReputation == null) { return NotFound(); } return View(characterReputation); } public async Task<IActionResult> _RepEditPartial(int? id) { return PartialView(); }
Here are my views:
Edit.cshtml: @model WebAppMVC.Models.CharRepViewModels.CharRepViewModel @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_CharEdLayout.cshtml"; }<h2 style="text-align: center; font-family: 'Times New Roman', Times, serif">Character Database Character Reputation Edit Form</h2><form asp-controller="CharReps" asp-action="Edit" method="post"><div class="form-horizontal"><h4>Character Reputation</h4><hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" })<b style="text-align: center; font-family: 'Times New Roman', Times, serif; font-size: x-large">@Html.DisplayFor(model => model.Name.CharFullName)</b><div class="form-group"><label asp-for="@Model.Reputation" class="control-label col-md-2"></label> @Html.Partial("_RepEditPartial", @Model.Reputation)</div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Save" class="btn btn-default" /></div></div></div></form><div> @Html.ActionLink("Back to Edit Character", "EdChar", "Menu")</div> _RepEditPartial.cshtml: @model IEnumerable<WebAppMVC.Models.CharRepViewModels.CharRepListViewModel> @{ } <div class="form-group"><div class="col-md-12"> @(Html .Grid(Model) .Build(columns => { columns.Add(model => model.CharID).Css("hidden"); columns.Add(model => model.FactionID).Css("hidden"); columns.Add(model => model.FactionName).Titled("Faction"); columns.Add(model => model.FactionSet).Titled("Available"); //This needs to be a editable checkbox that will enable the select list to open up if checked columns.Add(model => model.RepLevels.Select(a => a.RepLevelID).LastOrDefault()).Css("hidden"); //This needs to be part of a multi select list columns.Add(model => model.RepLevels.Select(a => a.RepIsChecked).LastOrDefault()); //This needs to be part of a multi select list, this will be what checks the item columns.Add(model => model.RepLevels.Select(a => a.RepLevelName).LastOrDefault()).Titled("Reputation"); //This needs to be part of a multi select list }) .Pageable() .Empty("No Data Found") )</div></div>
You will notice that currently I have .LastOrDefault in there for the last three entries as not having them in there would either give an error on the add, or display Linq on the view. This was so I could test if the data was making it to the view. Using the Html.CheckboxFor(model.FactionSet) gave an error on the checkbox for about unable to convert a bool to system.func<IEnumerable<ViewModel>,bool>', adding a new {@checked ="checked" } gave an error at the Add. Using @<input type="checkbox" asp-for="model.FactionSet" /> gave an error at the add. I used these both within the parenthesis after the lambda for model.
Any thoughts?