2015-01-29 1 views
0

테이블의 드롭 다운 메뉴에서 선택한 값을 얻을..... 내가 모든 클릭에 같은 값을 얻을 테이블의 드롭 다운 메뉴에서 값을 가져 오기 위해 시도입니다

<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="[email protected]"></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 코드는 다음과 같습니다

<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>} 
상단에

내가 시도 친군 특정 행 값을 가져 오기를 클릭 드롭 다운과 내가 필요 text.What 버튼에있는 테이블을 만들 합니다만

+0

'id'는 HTML에서 고유해야합니다. 그게 당신이 문제를 사용하는 이유는 클래스 선택기를 대신 사용 – Satpal

+0

'DropdownListCustom' 무엇입니까? 그것은'id' 속성을 중복하여 잘못된 html을 생성하는 것으로 보입니다 –

답변

3

이 시도 내가 (사용자 이름) 만 먼저 TD 값을 가져 액세스하려면 dropdownlist

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

Thanks gdy job :) –

관련 문제