skip to Main Content

I am completely new to a project I am to maintain.

Simply question: I have in my cshtml page (which I understand are razor pages?) set up a few checkboxes and a label to test the bound class behind it.

This I got to work:

@model Application.Areas.Cms.Models.ProduktBeispielViewModel 
<label>@Model.Test</label>

And the VM:

public string Test { get; set; } = "THIS IS A TEST";

And happy me: the words are displayed on my page. So the binding is working.

Now I put up a few checkboxes and once a submit button is pressed, I need to retrieve each checkbox and see if their value is checked or unchecked (shouldnt be too hard).

I first now just tried to display a value (eg true or false) from my VW onto my existing checkboxes.

This is what I did:

public bool Test2 { get; set; } = true;

CSHTML:

   <input type="checkbox" name="FoodTrends" value="@Model.Test2" />

I am seeing my checkbox, but it is unchecked.

1.) Why is my simple binding not working? is "value" not the right property?
2.) How would I retrieve my value from this checkbox

Thank you all!

Please have a look at this:

enter image description here

I am returning my model, with the value on Test2 being false

Now this is my exact code in my view:

<input type="checkbox" name="FoodTrends" value="@Model.Test2" checked="@Model.Test2" />

And the result is that the checkbox is checked, even though the value is set to false.

I have noticed also that my checkboxes are inside "<form>" tag.

EDIT:

Razorcode (briefly):

@model Application.Areas.Cms.Models.ProduktBeispielViewModel

@{
    ViewBag.PopupHeadline = "Produktbeispiele";
    ViewBag.PopupSubHeadline = Model.Item != null ? Model.Item.NameInCurrentLang : "";
    ViewBag.HideLanguageComparison = true;
}

@section TabMenu
{
    <ul>
        <li><a href="@Url.Action("Index", "ProduktbeispieleEditor", new { id = Model.Item.Id })" class="Active">Einstellungen</a></li>
        <li><a href="@Url.Action("Image", "ProduktbeispieleEditor", new { id = Model.Item.Id })">Bild</a></li>
    </ul>
}




<form action="@Url.Action("SaveIndex")" method="POST" id="idForm">
    @Html.HiddenFor(m => m.AutoCloseWindow)
    @Html.HiddenFor(m => m.Item.Id)

 <input type="checkbox" name="FoodTrends" value="@Model.Test2" />

</form>

2

Answers


  1. The checkbox wouldn’t work that way you have to use the checked attribute to get selected your checkbox.

     @model Practice_web.Models.TestModel
    
    @{
        ViewData["Title"] = "Home Page";
    
    }
    
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>
    
    
    
    <div>
        <input type="checkbox" name="FoodTrends" value="@Model.Test2" checked="@Model.Test2" />
    </div>
    

    Action Controller

     public IActionResult Index()
            {
                var model = new TestModel { Test2 = true };
                return View(model);
            }
    

    Here is the demo video link

    Demo Link

    Please try this. This works for me like a charm

    Login or Signup to reply.
  2. I don’t know if this is a bug or I just don’t understand how razor pages work but I have to new up the model first in the AddModel method then it seems to pick up the default value I set in the POCO for my checkbox bool property.

    [BindProperty]
    public NewMember? User { get; set; }
    public AddModel()
    {
        User = new NewMember();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search