2014-11-12 6 views
0

컨트롤러에서 JSON 형식으로보기로 데이터를 가져와야하지만 제대로 작동하지 않으며 그 이유가 없습니다.json으로 컨트롤러에서 데이터를 가져올 수 없습니다.

내 컨트롤러 (내가 index 액션에 요청) :

public class HomeController : Controller 
    { 
     CompanyTicketsDb _db = new CompanyTicketsDb(); 

     public ActionResult Index() 
     { 
      var model = 
       _db.Tickets 
        .Select(r => new TicketListViewModel 
        { 
         Id = r.Id, 
         Name = r.Name, 
         Description = r.Description, 
         CompanyName = r.CompanyName, 
         Status = r.Status, 
         CompanyId = r.CompanyId, 
        }); 
      if (Request.AcceptTypes.Contains("application/json")) 
      { 
       return Json(model, JsonRequestBehavior.AllowGet); 
      } 

      return View(model); 
     } 
    } 

그리고 내보기 (그것은 부분이다) 그 (테스트 모드) liek 같습니다

@model IEnumerable<MvcApplication4.Models.TicketListViewModel> 

<button id="Button1">asd</button> 

@section scripts { 
<script src="~/Scripts/knockout-3.2.0.js"></script> 
    <script> 
     $(document).ready(function() { 
      $("#Button1").click(function (evt) { 
       type: 'GET' 
       url = '/'; 
       dataType = 'json'; 
       contentType = 'application/json'; 
       success = function (data) { 
        alert(data); 
        console.log("asd"); 
       }; 
       error = function() { alert("Error retrieving employee data!"); }; 

      }); 
     }); 

    </script> 

} 

것은 JSON이 유효하기 때문에 테스트를 위해 어떤 메시지 (또는 allert 또는 콘솔 로그도)를 얻지 못한다면 View를 채우기 위해 사용할 수 있습니다.

+1

내 대답이 문제를 해결했다면 답을 표시하십시오. –

+0

해결되지 않았습니다. 다른 사람들에게 여러 가지 문제가 있습니다. 지금은 그것을 해결하려고합니다. 쉼표 및 ':'에 대한 구문 오류가 있으므로 $ .ajax ({- 오류는 발생하지 않지만 결과는 아직 얻지 못했습니다. < – CikLinas

+0

해결할 수있었습니다. 감사합니다. – CikLinas

답변

7

구문이 잘못 되었기 때문입니다. '='을 사용하고 있지만 ':'을 사용해야합니다.

@model IEnumerable<MvcApplication4.Models.TicketListViewModel> 

<button id="Button1">asd</button> 

@section scripts { 
<script src="~/Scripts/knockout-3.2.0.js"></script> 
<script> 
    $(document).ready(function() { 
     $("#Button1").click(function (evt) { 
      $.ajax({ 
       type: 'GET' 
       url : '/'; 
       dataType : 'json'; 
       contentType : 'application/json'; 
       success : function (data) { 
       alert(data); 
       console.log("asd"); 
      }; 
      error : function() { alert("Error retrieving employee data!"); }; 

     }); 
    }); 

</script> 

} 
관련 문제