2016-10-05 1 views
-4

여기 코드가 있습니다. 컨트롤러 메서드는 어떻게 호출 할 수 있습니까?드롭 다운 목록을 선택했을 때 컨트롤러 메서드를 호출하는 방법

<div class="uLeft"> 
    Select Institution: 
    @Html.DropDownList("Sortby", new SelectListItem[] 
    { 
     new SelectListItem() { Text = "Option1", Value = "1" }, 
     new SelectListItem() { Text = "Option2", Value = "2" }, 
     new SelectListItem() { Text = "Option3", Value = "3" }, 
     new SelectListItem() { Text = "Option4", Value = "4" } 
    }, 
    new { @onchange = "CallChangefunc(this.value)" }) 
</div> 
+2

당신은 컨트롤러 메서드 (당신은 게시물이나 get으로 표시되어야 함)를 사용하거나 jqu를 사용하여 CallChangefunc에서 ajax 호출을 작성합니다 당신이하고 싶은 일에 따라 에리 포스트 나로드 – Lidaranis

답변

0

변경 사항은 이미 CallChangefunc(this.value)입니다.

당신이 (포스트 예)이 함수를 선언하려고 다음 일 -

<script> 
    function CallChangefunc(value) { 
     $.post('http://localhost/{controller}/{action}', { sortBy: value }, function (data) { 
      console.log(data) 
     }) 
    } 
</script> 
0

이 드롭 다운 목록

@Html.DropDownList("Sortby", new SelectListItem[] 
{ 
     new SelectListItem() { Text = "Option1", Value = "1" }, 
     new SelectListItem() { Text = "Option2", Value = "2" }, 
     new SelectListItem() { Text = "Option3", Value = "3" }, 
     new SelectListItem() { Text = "Option4", Value = "4" } 
}, 
new { id = "ddlInstitution" }) 

에 ID를 제공하고이

$("#ddlInstitution").on('change', function() { 

    var selValue = $('#ddlInstitution').val(); 

    $.ajax({ 
     type: "POST", 
     url: "/{controller}/{action}", 
     dataType: "json", 
     data: { method parameter name : selValue }, 
     success: function (data) { 

     }, 
     failure: function (data) { 
      alert('oops something went wrong'); 
     } 
    }); 
}); 
처럼 jQuery 코드
관련 문제