简体   繁体   中英

How to use asp-for and conditional if in a checkbox using asp.net core?

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.

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:

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-5.0#c-in-tag-helpers-attributedeclaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM