Checkbox in lightbox returns False to controller in MVC 5

Hi,

I have a partial view in which there is a checkbox. This view serves as the content in my custom lightbox control. I used HTML helper to create this checkbox (i.e. @Html.CheckboxFor(m => m.IsActive)). The problem is when I click on the save button on the lightbox, it passed “False” to the controller all the time whether the checkbox is checked or not. Can anyone please shed some lights? Thanks!

Hello,
please give some code sample. The issue with the checkbox value may be related to MVC model binding mechanism, check discussion on SA
stackoverflow.com/questions/1473 … -net-mvc-4

Thanks for the response. Followings are my code snippets.

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MissMakita.Model.Models
{
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Web;

    [Table("Event")]
    public partial class Event
    {
        public int id { get; set; }

        [Column(TypeName = "text")]
        [Required]
        [DisplayName("Event Title")]
        public string text { get; set; }

        [Required]
        [DisplayName("Start Time")]
        public DateTime start_date { get; set; }

        [Required]
        [DisplayName("End Time")]
        public DateTime end_date { get; set; }

        [DisplayName("Is Active?")]
        public bool is_active { get; set; }
    }
}

View:

@model MissMakita.Model.Models.Event

@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <title>Appearance Scheduler Event Detail</title>
</head>
<body>
@using (Html.BeginForm("Save", "Home", new { id = "schedulerForm" }))
{
    @Html.HiddenFor(m => m.id)

    <div>
        @Html.DisplayFor(m => m.text)
        @Html.TextBoxFor(m => m.text)
    </div>
    <div>
        @Html.DisplayFor(m => m.start_date)
        @Html.TextBoxFor(m => m.start_date)
    </div>
    <div>
        @Html.DisplayFor(m => m.end_date)
        @Html.TextBoxFor(m => m.end_date)
    </div>
    <div>
        @Html.DisplayFor(m => m.is_active)
        @Html.TextBoxFor(m => m.is_active)
    </div>
    <div>
        <input type="submit" name="actionButton" value="Save" />
        <input type="button" onclick="lightbox.close()" value="Close" />
        <input type="submit" name="actionButton" value="Delete" />
    </div>
}
</body>
</html>

Controller:

public ContentResult Save(int? id, FormCollection actionValues)
{
   :
   :
}

is_active in the actionValues is always “false”.

Thanks!

Sorry, typos in the view, @Html.LabelFor should be used instead of @Html.DisplayFor.

Hi,
please check the SA topic from my previous reply. The code seems to work exactly the way as explained there.
In order to avoid this, you can either generate checkbox manually, or use mvc model binding to retreive the event public ContentResult Save(int? id, Event actionValues)
In this case probably you’ll need to set the invariant culture to your application in order to make sure that dates and numbers will be parsed the same way regardless the locale used on the server where you deploy app stackoverflow.com/questions/1100 … balization

Thanks, Aliaksandr! Replaced FormCollection with Event works for checkbox but now when I resize an existing event on the Calendar (i.e. reduce the time), the values posted back to the Event model has string text “NULL” for all string type properties instead of null reference. Please advise. Thanks!