Reputation: 207
I'm trying to use a conditional if and asp-for attribute in a checkbox but if I use both one of them doesn't work. I want to check the selected choices and I need asp-for for updating my list. This is my code:
@{
ViewData["Title"] = "EditRole";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
var i = 0;
}
<div class="col-lg-6">
@foreach (var all in Model.ActionAndControllerNames)
{
foreach (var selected in Model.SelectedActionAndControllerNames)
{
#region ToUpper selected
var action = all.ActionName.ToUpper();
var controller = all.ControllerName.ToUpper();
var area = "";
if (all.AreaName != null)
{
area = all.AreaName.ToUpper();
}
#endregion
<div class="form-group">
@{
var check = selected.AreaName == area &&
selected.ControllerName == controller && selected.ActionName ==
action?"checked":"";
}
<input type="checkbox" @((selected.AreaName == area &&
selected.ControllerName == controller && selected.ActionName
== action)?"checked":"") asp-
for="ActionAndControllerNames[i].IsSelected"/>
<input type="hidden" readonly="readonly" asp-
for="ActionAndControllerNames[i].ActionName" />
<input type="hidden" readonly="readonly" asp-
for="ActionAndControllerNames[i].ControllerName" />
<input type="hidden" readonly="readonly" asp-
for="ActionAndControllerNames[i].AreaName" />
<label for="@all.IsSelected">
@(Model.ActionAndControllerNames[i].AreaName ?? "NoArea") -
@Model.ActionAndControllerNames[i].ControllerName -
@Model.ActionAndControllerNames[i].ActionName
</label>
</div>
i++;
}
}
</div>
When I use it just the conditional if works and asp-for doesn't work and when I change their positions together it gives me error for the conditional if. I tried https://stackoverflow.com/a/54559960/13604077 way but it didn't work either. What should I do? EDIT This is the model:
public class EditRoleViewModel
{
public EditRoleViewModel()
{
ActionAndControllerNames = new List<ActionAndControllerName>();
}
[Required()]
public string RoleName { get; set; }
public string RoleId { get; set; }
public IList<ActionAndControllerName> ActionAndControllerNames { get;
set; }
public IList<ActionAndControllerName> SelectedActionAndControllerNames {
get; set; }
}
This is ActionAndControllerName class:
public class ActionAndControllerName
{
public string AreaName { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
public bool IsSelected { get; set; }
}
I want to edit the selected checkboxs so I need to show which one is checked so I need an if condition to compare all the checkboxes with selected ones.also I need to get the new selected list of checkboxes in the controller so I need asp-for.
Upvotes: 0
Views: 10707
Reputation: 36705
Tag Helpers do not allow C# in the element's attribute or tag declaration area.So what you did is impossible.
You could use checked="@check"
like below:
<input type="checkbox" asp-for="ActionAndControllerNames[i].IsSelected" checked="@check" />
But in your scenario,you could only change like below:
@if (selected.AreaName == area &&
selected.ControllerName == controller && selected.ActionName ==
action)
{
<input type="checkbox" asp-for="ActionAndControllerNames[i].IsSelected" checked />
}
else
{
<input type="checkbox" asp-for="ActionAndControllerNames[i].IsSelected" />
}
Reference:
Upvotes: 3