How to save radiobutton selected value in mvc when radio buttons are binded with database values?

时间:2016-02-03 04:26:10

标签: jquery entity-framework asp.net-mvc-4

I have radiobutton in my view. I am binding values to radiobutton from db. After selection using jquery i want to send selected value of radiobutton to controller. Here is my view code.

@{
    ViewBag.Title = "permmapping";
    Layout = "~/Views/Shared/_LayoutC3Card.cshtml";
    List<c3card.Dal.EDModel.ts_grp_perm_mapping> tmgrp1 = (List<c3card.Dal.EDModel.ts_grp_perm_mapping>)ViewBag.SelectedCheckBoxes;
}

script type="text/javascript">
    $.ajax({
        type: 'POST',
        url: '/Grpcontroller/SaveNewUser',
        data: { usr_groupId: $('#ddl_group').val() },
        dataType: 'json',
        success: function (data) {
            alert('User saved successfully.');
            $('#tblUserRegistration').empty();
        }
    });

</script>

This is my table

<table id="tblUserRegistration">
    <tr>
        <td>
            <div class="form-box form-box-default">
                @Html.DropDownList("ddl_group", ViewBag.list as SelectList, "--Select--")
            </div>
        <td>
    </tr>
    <tr>
        <td>
            <div>
                <ul>
                    @{ int row = 0; }
                    @foreach (c3card.Dal.EDModel.tm_perm_level obj in tmgrp)
                    {
                        <li>@Html.RadioButton("MID",obj.perm_id)@obj.perm_levelname</li>
                    }
                </ul>
            </div>
        </td>
    </tr>
</table>

Here i want to get selected radiobutton id which i want to send it to controller using javascript.

This is my controller code.

public ActionResult Index()
{
    List<tm_perm_level> tm_perm_level = GrpPermBAL.GetAllPermType();
    ViewBag.chklist = tm_perm_level;
    // List<tm_grp_group> tm = UserService.GetAllGroup();
    ViewBag.list = new SelectList(GrpPermBAL.GetAllGroup(), "grp_id", "grp_name");
    return View("~/Views/Grpcontroller/permmapping.cshtml");
}

public JsonResult SaveNewUser(int usr_groupId,string usr_doctypeids)
{
    List<ts_grp_perm_mapping> ts_grp_perm_mapping = GrpPermBAL.GetData(usr_groupId);
    ViewBag.SelectedCheckBoxes = ts_grp_perm_mapping;     
    int result = GrpPermBAL.saveNewUser(usr_groupId,usr_doctypeids);
    return Json(result, JsonRequestBehavior.AllowGet);
}

1 个答案:

答案 0 :(得分:0)

要获取所选单选按钮的值,您可以使用

var selected = $('[name="MID"]:checked').val());

然后ajax data选项变为

data: { usr_groupId: $('#ddl_group').val(), usr_doctypeids: selected },

会将所选选项的值绑定到string usr_doctypeids方法中的SaveNewUser()参数