0

I am having trouble passing the value of the selected radio button to my controller. Whatever radio button i have listed first is the value i get back to my controller. For example "tst" is always returned

VIEW WITH JQUERY

<div class="atk-radio-center">
            <div>                   
                    @Html.RadioButtonFor(m => m.Impact, "tst", new { Checked = "checked" })
                    Yes
                    @Html.RadioButtonFor(m => m.Impact, "0")
                    No
            </div>
        </div>
<script>
$("#btnContinue").click(function () {
    $.post("/Start/SaveServiceAffectd",
        {
            impact: $('#Impact').val(),
            description: $('#Description').val(),

        },
        function (data) {
            if (data == "true") {

            } else {

            }
        });
});

CONTROLLER

 [HttpPost]
    public bool SaveServiceAffectd(string impact, string description)
    {
        try
        {
            var jobs = new JobRepository();
            jobs.UpdateUserJob(PCSSession.Current.CurrentJob.id, impact, description);
            return true;
        }
        catch(Exception ex)
        {
            return false;
            throw ex;              
        }


    }

VIEW MODEL

    [Required(ErrorMessage = "Work Description is required")]
    [Display(Name = "Description")]
    public string Description { get; set; }

    [Required(ErrorMessage = "Answer for Impact is required")]
    public string Impact { get; set; }
Ha66y-Day
  • 197
  • 1
  • 2
  • 12
  • http://api.jquery.com/checked-selector/ Select the radio button that is checked. – Taplar Jul 24 '18 at 18:16
  • Also, ids should not be repeated. They are expected to be unique by web standards. Use a class instead. https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page – Taplar Jul 24 '18 at 18:17
  • 1
    a better way to do this would be to wrap everything in a form and [serialize()](https://api.jquery.com/serialize/) the form to send it to the controller. jquery form.serialize() will do the right thing when it comes to checkboxes and radiobuttons. – Fran Jul 24 '18 at 18:23
  • If you were to post the *form* then your form data would only include the checked value, by `name=` attribute (not by `id=`). Because you're generating the data manually, you need to emulate that: `$("[name=Impact]:checked").val()` – freedomn-m Jul 24 '18 at 18:25
  • @freedomn-m RadioButtonFor creates the correct html with a name attribute. You could also use 'input:checked' as the selector – Fran Jul 24 '18 at 18:29
  • @Fran correct, but OPs is using `$("#Impact")` which uses id not name. The HTML is fine, it's OPs js code that's the issue. – freedomn-m Jul 24 '18 at 18:43
  • @freedomn-m yes. and I'm saying don't call the elements by id and use a form since he seems to be using all the form semantics without actually using one. – Fran Jul 24 '18 at 18:54
  • $("[name=Impact]:checked").val() is what i needed for this to work – Ha66y-Day Jul 24 '18 at 19:02

1 Answers1

1
impact: $("input[name='Impact']:checked").val(),

This is what i needed to change my JQUERY to in order to pass correct value

Ha66y-Day
  • 197
  • 1
  • 2
  • 12