1
//Part of View
    <div class="checkbox">
        <label id="sendEmail" class="" >
            <input id="sendEmailInput" type="checkbox" asp-for="CompanySetting.SendEmail">Send mail for jobs.
        </label>
    </div>


//Model
    public partial class CompanySetting
    {
        public int CompanyId { get; set; }
        public bool SendEmail { get; set; }

        public virtual CompanyUsers Company { get; set; }
    }
//Controller
    public ActionResult AccountSettingSave(CompanyAccountViewModel model)
    {
        try
        {
            CompanySetting settingModel = _companySettingService.GetByCompanyId(GetId());
            settingModel.SendEmail = model.CompanySetting.SendEmail;
            _companySettingService.UpdateBill(settingModel);
            TempDataMessage("message", "Hesap ayarları başarıyla güncellendi.");
            return RedirectToAction("AccountSettings");
        }
        catch
        {
            TempDataMessage("message", "Hesap ayarları güncellenemedi.");
            return RedirectToAction("AccountSettings");
        }

    }

Checbox does not affect model, it is always default value. probably checkbox returns just on. but i tried with javascript + extra one input and works . i dont want use javascirpt control

6
  • 1
    Looks like a perfectly fine checkbox to me. Please be more precise. What does not work? Does it not render? Does is not pass information from or to the controller? Do you receive exceptions? Commented Jul 28, 2017 at 7:32
  • @Marco sorry , this checkbox does not affect Model.CompanySetting.SendEmail. it is always default value Commented Jul 28, 2017 at 7:51
  • Please show us your model, controller and view. We need a verifiable example Commented Jul 28, 2017 at 8:02
  • And as a hint: If you have a controller action for a specific model. Let Visual Studio do the brunt work for you and scaffold the view bx right clicking onto your action method and selecting "Add View". Whie this will not have the styling you need, it at least gives you a working starting point in seconds Commented Jul 28, 2017 at 8:14
  • @Marco edited and answered a solution . can you examine solution ? Commented Jul 28, 2017 at 8:33

2 Answers 2

1

This is because when checkbox is checked/unchecked then the property 'value' is not changing, it is changing 'data-val'. Which is why the changes are not binding to the model.

You could use your workaround. I used below much simpler solution, which transfers Property value of data-val to value via javascript.

<input asp-for="@Model.sendMail" type="checkbox" value="@Model.sendMail" />&nbsp;Approve?

<script>
$('#sendMail').on('change', function () {
   $('#sendMail').val($('#sendMail').attr('data-val'));
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

0
<div class="checkbox">
    <input id="sendMail" type="text" asp-for="CompanySetting.SendEmail" style="display: none" />
    <label id="sendMailLabel"><input  type="checkbox">Send Mail for Jobs</label>
</div>

if ($('#sendMail').val() == 'True') {
    $('#sendMailLabel').addClass('checked');
} else {
    $('#sendMailLabel').removeClass('checked');
}
$('#sendMailLabel').on('change', function () {
    if ($('#sendMailLabel').hasClass('checked')) {
        $('#sendMail').val('True');
    } else {
        $('#sendMail').val('False');
    }
});

this code works. do i should use it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.