2011-08-09 4 views
0

바구니에 추가 된 제품의 총량을 보여주는 총 제품 페이지가 있습니다. 사용자가 할 수있는 프로모션 텍스트 필드를 추가하고 싶습니다. 할인을 받기 위해 프로모션 코드를 추가하면 프로모션 코드가 할인율로 데이터베이스에 저장됩니다. 따라서 고객이 코드를 입력하고 프로모션 버튼을 누르면 시스템은 유효한 프로모션 코드를 확인한 다음 총 가격을 할인율로 공제하여 순간 계산을 수행해야합니다. 이 작업은 jQuery Ajax를 사용하여 수행 할 수 있습니까? 아니면 다른 가능한 솔루션이 있습니까? 모든 도움말 또는 자습서를 매우 높이 평가할 것입니다. 건배mvc2의보기에서 jQuery의 Ajax를 사용하여

<script language="javascript" type="text/javascript"> 

     $(document).ready(function() { 
      $("#promo").change(function() { 
       var $this = $(this); 
       var number = $('promoNumber').val(); 
       $.ajax({ 
        url: '/Booking/Review', 
        type: "POST", 
        data: {number:textbox}, 
        success: function (data) { 
            if (data["success"]) { 
            alert(data) 
            } 
        } 
       }); 
      }); 
     }); 
    </script> 

    <%using (Html.BeginForm()) 
{ %> 
     <table> 
      <tr> 
       <td> <input type="button" id="promo" value="Promo" /> </td> 
       <td> <%: Html.TextBox("promoNumber") </td> 
       </tr> 
       <tr><td> 
        <div class="totalCost"> 
        <label><b>Total Amount:</b></label> <%: String.Format("{0:c}", ViewBag.TotalAmount)%> <br /></div> 
       </td></tr> 

       </table> }%> 

컨트롤러

[HttpPost] 
    public ActionResult Review(int number)//this parameter is my selected checkbox value.I have to get this value from ajax 
    { 
     IVoucherRepository voucherResp = new VoucherRepository(); 
     IQueryable<Voucher> getVoucher = voucherResp.GetVouchers(number); 
     //first check if the code is there or not else return error 
     int discount = 0; 
     foreach (var number in getVoucher) 
     { 
     discount = number.discount;//Thats the discount number 
     } 
     //after getting the discount code i need to pass that to ViewBag.TotalAmount to do calculation 
     ViewData["Data"] = "success"; 
     return View(); 
    } 
+1

이것은 확실히 Javascript/AJAX로 수행 할 수 있습니다. jQuery는 좋은 출발점이 될 것이다. 또는 Knockout.js는 주로이 목표를 달성하는 데 기반을 둔 프레임 워크입니다. 먼저 프레임 워크를 선택하고 그 프레임 워크를 가지고 놀아야합니다. 보유하고있는 코드에서 관련 코드를 가지고 있습니까? – ghayes

+0

asp.net MVC를 사용하고 있습니다 –

+1

네,하지만 (태그에서) jQuery도 사용하고 있습니다. 근본적으로, 예, 사용자가 쿠폰 코드를 입력하면 관련 가격 할인을 반환하고 필드를 적절히 업데이트하는 AJAX 요청을 시작할 수 있습니다. 특정 질문에 대한 답변은 다른 관련 코드없이 제공 될 수 있습니다. – ghayes

답변

1

난 당신이 컨트롤러에 데이터를 게시하는 문제를 본 적이?
당신은 jQuery를이처럼 promoNumber을 참조해야합니다 :

var number = $('#promoNumber').val(); 

이 내가 할 것 인 것이다 : 당신은 내가 이벤트를 클릭하여 사용합니다 버튼을 사용하고 있기 때문에

$(document).ready(function() { 
    $("#promo").click(function() { 
     $.ajax({ 
      type: 'POST', 
      url: 'Booking/Review', 
      dataType: 'json', 
      data: { number: $("#promoNumber").val() }, 
      success: function(data) { 
       if (data) { 
        alert(data.discount); 
        alert(data.totalAmount); 
       } 
      } 
     }); 
    }); 
}); 

.
jSon을 반환 객체로 사용하고 있습니다. 당신은 모든 계산을 한 다음 원하는 모든 속성과 JSON 개체를 반환 할 수 있습니다 컨트롤러에

[HttpPost] 
public ActionResult Review(int number) 
    { 
    IVoucherRepository voucherResp = new VoucherRepository(); 
    IQueryable<Voucher> getVoucher = voucherResp.GetVouchers(number); 
    //first check if the code is there or not else return error 
    int discount = 0; 
    int totalAmount = 0; 
    foreach (var number in getVoucher) 
     { 
     discount = number.discount;//Thats the discount number 
     } 
    //after getting the discount code i need to pass that to ViewBag.TotalAmount to do calculation 

    return (Json(new { discount = discount, totalAmount = totalAmount }, JsonRequestBehavior.DenyGet)); 

    } 

:

는이 컨트롤러입니다.

0

은 쿠폰 버튼에 jQuery를 포스트 이벤트를 첨부 : ghayes처럼

$('input[name="coupon"]').click(function() { 
    $.ajax({ 
     type: 'POST', 
     url: myUrl, 
     data: {code: $('input[name="code"]').val()}, 
     success: function(discount) { 
      /* logic here. may need to store the code and it's discount 
       if more than one code is allowed */ 
     } 
    }); 
}); 

은 기존 솔루션이있는 경우, 그를 사용하지만 언급했다. 그들은 대부분 당신에게 알려지지 않은 많은 상황을 고려했을 것입니다.

var number = $('promoNumber').val(); 

다음 :

data: {number:textbox}, 

텍스트 상자는 무엇입니까

+0

질문을 업데이트했습니다. –

관련 문제