2016-11-28 2 views
0

asp.net MVC를 처음 사용합니다. 나는 MVC 뮤직 스토어에서 일하고있다. 나는 거의 부분을 코딩하는 것으로 끝났지 만, 카트 부분에서 항목을 제거하는 동안 붙어있다. ,카트에서 제품 제거가 MVC 뮤직 스토어 (AJAX)에서 작동하지 않습니다.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
 
<script type="text/javascript"> 
 
    if (typeof jQuery == 'undefined') { 
 
     document.write(unescape("%3Cscript src='/Scripts/jquery-1.7.1.min.js' type='text/javascript'%3E%3C/script%3E")); 
 
    } 
 
</script> 
 
<script type="text/javascript"> 
 
    $(function() { 
 
     // Document.ready -> link up remove event handler 
 
     $(".RemoveLink").click(function() { 
 
      // Get the id from the link 
 
      var recordToDelete = $(this).attr("data-id"); 
 
      if (recordToDelete != '') { 
 
       // Perform the ajax post 
 
       $.post("/ShoppingCart/RemoveFromCart", {"id": recordToDelete }, 
 
        function (data) { 
 
         // Successful requests get here 
 
         // Update the page elements 
 
         if (data.ItemCount == 0) { 
 
          $('#row-' + data.DeleteId).fadeOut('slow'); 
 
         } else { 
 
          $('#item-count-' + data.DeleteId).text(data.ItemCount); 
 
         } 
 
         $('#cart-total').text(data.CartTotal); 
 
         $('#update-message').text(data.Message); 
 
         $('#cart-status').text('Cart (' + data.CartCount + ')'); 
 
        }); 
 
      } 
 
     }); 
 
    }); 
 

 
    function handleUpdate() { 
 
     // Load and deserialize the returned JSON data 
 
     var json = context.get_data(); 
 
     var data = Sys.Serialization.JavaScriptSerializer.deserialize(json); 
 

 
     // Update the page elements 
 
     if (data.ItemCount == 0) { 
 
      $('#row-' + data.DeleteId).fadeOut('slow'); 
 
     } else { 
 
      $('#item-count-' + data.DeleteId).text(data.ItemCount); 
 
     } 
 

 
     $('#cart-total').text(data.CartTotal); 
 
     $('#update-message').text(data.Message); 
 
     $('#cart-status').text('Cart (' + data.CartCount + ')'); 
 
    } 
 
</script>
다음

액션 링크

<td> 
      <a href="#" class="RemoveLink" data-id="<%:item.RecordId %>">Remove from cart</a> 
     </td> 

http://localhost:14652/ShoppingCart# 내가 제거 카트 링크를 클릭

입니다 :

내가보기 페이지에서 사용하는 코드는 다음과 같다 이 URL로 바뀝니다.

// 
    // AJAX: /ShoppingCart/RemoveFromCart/5 
    [HttpPost] 
    public ActionResult RemoveFromCart(int id) 
    { 
     // Remove the item from the cart 
     var cart = ShoppingCart.GetCart(this.HttpContext); 

     // Get the name of the album to display confirmation 
     string albumName = storeDB.Carts 
      .Single(item => item.RecordId == id).Album.Title; 

     // Remove from cart 
     int itemCount = cart.RemoveFromCart(id); 

     // Display the confirmation message 
     var results = new ShoppingCartRemoveViewModel 
     { 
      Message = Server.HtmlEncode(albumName) + 
       " has been removed from your shopping cart.", 
      CartTotal = cart.GetTotal(), 
      CartCount = cart.GetCount(), 
      ItemCount = itemCount, 
      DeleteId = id 
     }; 
     return Json(results); 
    } 

친절히 도와주세요.

+0

앵커 태그의 href에서 "#"때문에 URL이 변경되고 있습니다. 단추로 변경하거나 href 특성을 제거 할 수 있습니다. 카트에서 제거하십시오. 그러나 $ (". RemoveLink") .align 문에 경고 문구를 넣어야 click() 처리기가 제대로 바인딩되었는지 알 수 있습니다. 아니면 중단 점을 사용하여 크롬 개발자 도구에서 테스트 할 수 있습니다. –

+0

리소스를로드하지 못했습니다 : 서버가 404 상태로 응답했습니다 (찾을 수 없음) – user7090664

+0

404가 표시됩니다. 버튼 클릭 이벤트 또는 개발자 도구의 n/w 탭에서? 컨트롤러 이름과 작업 메서드 이름이 click 이벤트에서 제공하는 URL과 일치하는지 확인 했습니까? –

답변

0

같은 문제가있었습니다. 나는

<script src="/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script> 

<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> 

에서 다음 줄을 변경했고, 모든 그 후했다. 나는 그것이 매우 간단했다라고 생각할 수 없었다.

관련 문제