mardi 19 mars 2019

Checkbox always pass true value to controller MVC .Net

I'm using a checkbox to handle allow Ordering function, which generates HTML code below:

@using (Html.BeginForm("EditFarm", "CustomerManagement", FormMethod.Post))
{
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)
  @Html.HiddenFor(model => model.AllowOrder)

 <div>
   <label for="allowOrder">Allow Ordering</label>
   <br />
   <input type="checkbox" id="allowOrder" name="AllowOrder" value="true" @(Model.AllowOrder ? "checked" : "") />
 </div>
....
}

The problem is checkbox AllowerOrder always pass true value to Controller even when it's unchecked

This is code in Controller:

public ActionResult EditFarm([Bind(Include = "Id, ERPCustomerCode, ERPId, CategoryId, Size, Large, OtherId, AllowOrder")] NewFarm farm)
        {
           ...
        }

Tried the debug, received model always have AllowerOrder = true. enter image description here

Is there any issue in my code? Any help would be appreciated! Thanks!




Aucun commentaire:

Enregistrer un commentaire