checkbox.checked is always false. Net Core 6.

Volk Volk 551 Reputation points
2022-09-25T16:26:44.033+00:00

Hi!

You recently helped me solve the problem with the checkbox here: If I set asp-for, the stylized checkbox disappears. Net Core 6.
Everything works fine, but in a different situation on another page with the same code, but with a different model (the model is shown below) checkbox.checked is always false.
I want to pass the current state of the checkbox to the controller, but it is always false, even if I turn it on.

I can't write checked="@默 .SendNews" like this, since I need to pass additional data to Model. But I tried this option @默 .SendNews, it still doesn't work.

Even if at the start of the page @默 .User.SendNews = true (the checkbox is activated correctly), it's still asp-route-appuser="@默 .User" SendNews is always false.

Tell me, what could be the problem?

Thanks!

Page checkbox:

<div class="form-check">  
                                 <i class="icon solid fa-question-circle" style="color: deepskyblue" title=" @language.Getkey("get_news_and_updates")"></i>  
                                 <input type=checkbox name="SendNews" checked="@Model.User.SendNews" value="true" class="form-check-input" id="checkNews"/>  
                                 <label class="form-check-label" name="SendNews" for="checkNews">@language.Getkey("news")</label>  
                            </div>  
  
<form asp-area="Customer" asp-controller="Inventory" asp-action="Buy" asp-route-appuser="@Model.User">  
                                    <input id="BuyButton" type="submit" disabled class="button large" value="@language.Getkey("buy")"/>  
                                </form>  

Model:

using Project.Models;  
  
namespace Project.Areas.Customer.Models  
{  
 public class CartDetailModel  
 {  
        public List<Products>? Products { get; set; }  
        public List<Likes>? Likes { get; set; }  
        public ApplicationUser? User { get; set; }  
    }  
}  

Controller method (appuser.SendNews is always false):

[ValidateAntiForgeryToken]  
        public async Task<IActionResult> Buy(ApplicationUser appuser)  
        {  
  
}  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,685 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.