Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I got two radio button and using entity framework, they not saving to local db records. Let me share the code below

What I have tried:

// Model
C#
public class Application_Check 
{
    public bool PLC_Marshal { get; set; }
    public bool BVMS_PMON { get; set; }
}


// Controller
C#
// radio_button to save values to the database.
[HttpPost]
public ActionResult SelectedValuesSaved(Application_Check checkList)
{
    if(ModelState.IsValid)
    {
        var selectedAnswer = checkList.SelectedAnswer;
    }
    return Json("data saved", JsonRequestBehavior.AllowGet);
}


// View
HTML
<!--Saving values to the local database Incident Template-->
@using (Html.BeginForm())
{

    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Application CheckList</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">

            <div class="col-md-10">
                PLC_Marshal
                @Html.RadioButtonFor(model => model.Application_Check, "PLC_Marshal")
                <br />
                BVMS_PMON
                @Html.RadioButtonFor(model => model.Application_Check, "BVMS_PMON")
                <br/>
                <br/>
                <button type="button" class="btn btn-default" id="btnSubmit">Submit</button>
            </div>
        </div>
        </div>
        }

JavaScript
// This method submit data to records.
//Radio button option to store value.
$(document).ready(function () {
    //var selectsList = $('input[name="PLC_Marshal"]:checked').val();
    //var selectsList = $('"PLC_Marshal"]:checked').val();
    $("#btnSubmit").click(function (e) {
        debugger;
        var checkList = {
            PLC_Marshal: $("#PLC_Marshal").val()
        }

    });

});

$.ajax({
    type: "POST",
    url: "Home/SelectedValuesSaved",
    data: {},
    dataType: "json",
    cache: false,
    success: function () {
        alert("Saved Successfull")
    }
});
Posted
Updated 26-Nov-21 0:42am
v4

1 solution

Radio buttons are for allowing the user to pick one of a mutually-exclusive set of options. You create one radio button for each option, and they all have the same name.

Given your model, you want to use checkboxes instead.
Razor
@Html.CheckBoxFor(model => model.Application_Check, "PLC_Marshal")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900