Richard Ray
Richard Ray

Reputation: 67

get selected value from dropdown in a table

i am try to fetch the values from dropdown in a table i get same value on every click.....

 <tbody>

        @{
            var tempId=1;
            foreach (var item in lst)
            {
                var username = _lst.Where(x => x.UserName == @item.UserName);
                if (username.Count() > 0)
                {
                    <tr>
                        <td><input type="hidden" id="@tempId" name="tempId_@tempId"></td>
                        <td>@username.Select(x => x.UserName).FirstOrDefault() </td>
                        <td class="nr"> @*@Html.DropDownList("UserRoleTypeId", "Select")*@

                        @Html.DropdownListCustom("UserRoleTypeId" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId)
                        </td>
                        <td>
                            @Html.Hidden("Id", i = username.Select(x => x.Id).FirstOrDefault())
                        </td>
                        <td><input type="button" data-id="@item.UserName" value="set" class="btn-update" />  </td>
                    </tr>
                }
                else
                {
                    <tr>
                        <td></td>
                        <td>@item.UserName</td>
                        <td class="nr"> @*@Html.DropDownList("UserRoleTypeId", "Select")*@
                            @*@Html.DropdownListCustom("UserRoleTypeId_" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId, new { @id = "UserRoleTypeId" })*@
                       @Html.DropdownListCustom("UserRoleTypeId" + @tempId, "", (IEnumerable<SelectListItem>)ViewBag.UserRoleTypeId)
                        </td>
                        <td>
                            @Html.Hidden("Id", i)
                        </td>
                        <td><input type="button" data-id="@item.UserName" value="set" class="btn-update" /></td>
                    </tr>
                }
                tempId = tempId + 1;
            }
        }

    </tbody>

jquery code is:

 <script type="text/javascript">
        $(function () {
            //var UserRoleTypeId = 0;
            $('.btn-update').click(function () {
                debugger;
                var element = this;
                var root = '@Url.Content("~")';
                UserRoleTypeId = $("#UserRoleTypeId option:selected").val();
                var Id = $("#Id").val();
                alert(UserRoleTypeId);
                $.ajax({
                    type: 'Get',
                    url: root + 'AssginRole/UpdateRole',
                    data: { 'userId': $(this).attr('data-id'), 'UserRoleTypeId': UserRoleTypeId, 'Id': Id },
                    dataType: 'json',
                    success: function () {
                        $('#loadingElement').hide();
                        alert("Permission Set Sucessfully");
                    },
                    error: function (req, status, err) {
                        alert('unable to update the role, sorry, pls. try again... ' + err);
                        $('#loadingElement').hide();
                    }
                });
            });
        });
    </script>}

on Top i'm create a table where i have dropdown and text.What i need is on button click get particular row value .I'm try but i get only first td value(username)

Upvotes: 0

Views: 5380

Answers (1)

Manoj
Manoj

Reputation: 5071

Try this to access dropdownlist

 UserRoleTypeId = $(this).parent().siblings().find("select").val();

Upvotes: 4

Related Questions