2012-12-15 5 views
0

에 나는 JQuery와에 내가 아주 간단한 예제를 만들기 위해 노력하고채우기 JQuery와 콤보 상자는 MVC3

MVC 3에 새로운입니다.

jquery 콤보 박스가 있는데 페이지로드 중에 일부 데이터로 채워 넣고 싶습니다.

클라이언트

$(function() { 
     $("#combobox").combobox({ 
     // initialValues: ['array', 'of', 'values'], 
      source: function (request, response) { 
       if (!request.term.length) { 
        response(_self.options.initialValues); 
       } else { 
        if (typeof _self.options.source === "function") { 
         _self.options.source(request, response); 
        } else if (typeof _self.options.source === "string") { 

         $.ajax({ 
          url: "/dropdown/GetList", 
          //data: request, 
          dataType: "json" 
          //success: function (data, status) { 
          // response(data); 
          //}, 
          //error: function() { 
          // response([]); 
          // } 
         }); 



        } 
       } 
      } 

     }); 
     $("#toggle").click(function() { 
      // $("#combobox").toggle(); 
     }); 
    }); 


**Function in Controller** 
[HttpGet] 
     public JsonResult GetList() 
     { 
      try 
      { 
       Employee objName = new Employee(); 
       objName.Name = "Test"; 
       List<Employee> objectList = new List<Employee>(); 
       objectList.Add(objName); 
       return Json(new { Records = objectList, TotalRecordCount = 1 }); 
      } 
      catch (Exception ex) 
      { 
       return Json(new { Result = "ERROR", Message = ex.Message }); 
      } 
     } 

내가 서버 측 함수에서 중단 점을 넣어 가지고 있지만, 거기에 도달하지 :

그래서 여기에 코드입니다. 정말 감사드립니다. 당신의 URL에 대해서는 고급의

감사합니다, V

답변

0

. 시도하십시오 dropdown/GetList이 작동합니다.

또한 this article을 읽어보십시오. 여기

웹 API 코드

컨트롤러 코드

public ActionResult Test() 
{ 
    string test = "Hello World"; 
    return Json(test, JsonRequestBehavior.AllowGet); 
} 

에게, 내가 가지고 올 수있는 간단한 예입니다

public string Test() 
{ 
    string test = "Hello World"; 
    return test; 
} 

JQuery와 코드

,
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $.ajax({ 
      url: "Home/Test", 
      dataType: "json", 
      success: function (result) { 
       console.log(result); 
      } 
     }); 
    }); 
</script> 
+0

안녕하세요, 답장을 보내 주셔서 감사드립니다. –

+0

URL과 actioname은 모두 정확합니다. 그러나, 내가 방금 추천 한 기사를 읽을 것입니다. –

+0

예, 저도 노력했습니다. 문제는 그것을 콤보 상자에 바인딩하는 방법입니다. –

0

아래의 방법으로 데이터를 드롭 다운/콤보 상자에로드하는 데 사용됩니다. 지정된 URL이 정확한지 확인하십시오. 'Controller'의 URL에서 지정한 ActionResult를 호출하고 ActionResult에서 Json을 반환하여 데이터를로드합니다.

스크립트

<script type="text/javascript"> 
    var dropDownOptions = null; 
    function GetdropDownData(sender, args) { 
     $.ajax({ 
      type: "POST", 
      url: "Home/GetCountries", 
      dataType: "json", 
      success: function (data) { 
       dropDownOptions = data; 

      } 
     }); 
    } 

    </script> 

컨트롤러

public ActionResult GetCountries() 
    { 
     return Json(CountryList, JsonRequestBehavior.AllowGet); 

    } 


public IEnumerable<SelectListItem> CountryList 
    { 
     get 
     { 
      // load data here 
      return type; 
     } 
    } 
관련 문제