1

I found answers to similar questions in many places but it didn't work with me even by copy/paste the code!

I am developing an MVC/C# application and I have a I have a bool property like the following:

public bool IsMarried { get; set; }

On the view side, I have the following code to represent the property:

<label class="radio-inline">
    @Html.RadioButtonFor(model => model.IsMarried , true)Yes
</label>
<label class="radio-inline">
    @Html.RadioButtonFor(model => model.IsMarried , false)No
</label>

My problem is whwnever I submit the form, I get all of the rest of the normal fields (text) except the boolean property is always true

As I mentioned, there are many solutions I found such as this one but I don't know why it didn't work for me.


Added by author:

I wanted to add to this question to make everything visible. In addition to the code I am showing above, I am using Ajax to submit the form.

This is the Ajax code:

function SaveWebFormToDatabase() {
    var formData = new FormData();
    //Append the other textboxes with the form data
    formData.append("LastName", $("#LastName").val());
    formData.append("FirstName", $("#FirstName").val());
    formData.append("IsMarried", $("#IsMarried").val());

    $.ajax({
        type: "POST",
        url: "../User/SaveFirstWebForm",
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
            $('#loading_div').hide();
        },
        error: function (exception) {
            console.log(exception);
        }
    });
}

In the Controller side, this is the method that receives the call:

[HttpPost]
    public JsonResult SaveFirstWebForm(WebApplicationFirstPhase model)
    {
        string Nme = model.FirstName + model.LastName;
        bool ArmedFBit = model.IsMarried;//this is always true!
        return Json("");
    }
Ahmed Ali
  • 127
  • 2
  • 13
  • 2
    As far as creating radio buttons for a `bool` property in your model, I think that looks right. Have you verified that these elements are in the form? `@using (Html.BeginForm(...)) { //form elements in the using block }` – chadnt Aug 29 '18 at 20:13
  • @chadnt I did, but let me double check – Ahmed Ali Aug 29 '18 at 20:20
  • @chadnt yes, everything looks correct in the form and it is still passing true even when I click No – Ahmed Ali Aug 29 '18 at 20:27
  • 1
    What you claiming is not possible based on the code you have shown - its due to code you have not shown (one possible cause would be if you have included a hidden input for `IsMarried` before the radio buttons) –  Aug 29 '18 at 21:56
  • @StephenMuecke thank you, I added the other code to show you everything related to this property. Nothing hidden in the form, I hope I was able to explain everything. – Ahmed Ali Aug 29 '18 at 22:11
  • Thanks, I am glad I was able to explain the issue. – Ahmed Ali Aug 29 '18 at 22:16

1 Answers1

1

The problem is in the jquery selector setting.

You can try to use

$('input[name=IsMarried]:checked').val()

instead of

$("#IsMarried").val()

get the value, because you want to get radio button, which was checked.

D-Shih
  • 44,943
  • 6
  • 31
  • 51