2011-08-09 39 views
0

嗨,我正在使用Ajax Jquery將促銷代碼傳遞給控制器​​, 檢查代碼,做一些計算並返回結果,我想要做什麼 是更換新的結果頁面上的總的結果,下面是代碼:如何在mvc 2中使用jquery Ajax替換值另一個值

("#deduct").click(function() { 
     var codenumber = $('#codeText').val(); 
     $.ajax({ 
      type:'POST',  
      url: this.href, 
      cache: false, 
      data: {input:$('#codeText').val() }, 
      success: function (result) { 
      alert(result.name); 
     }, 
     error: function() { 
      alert("error"); 
     } 
     }); 
     return false; 
    }); 


    <div class="totalCost"> 
        <label><b>Amount:</b></label> <%: String.Format("{0:c}", ViewBag.TotalAmount)%> <br /></div> // Want to replace ViewBag.TotalAmount with json result 
        <div class="promo"> 
        <%:Html.ActionLink("promo", "Deduct", "Booking", null, new { id = "deduct" })%><input type="text" name="" id="codeText" /> 

        </div> 

控制器:

[HttpPost] 
    public ActionResult Deduct(string input) 
    { 
     IVoucherRepository voucherResp = new VoucherRepository(); 
     IQueryable<Voucher> getVoucher = voucherResp.GetAllVouchers(); 
     //first check if the code is there or not else return error 

     if (input == "") 
     { 
      return Json(new { name = "Please Enter the Promo Code" }); 
     } 
     else if (getVoucher.Any(r => r.Code == input)) 
     { 
      foreach (var validCode in getVoucher) 
      { 
       if (validCode.DiscountType == 1) 
       { 
        decimal discount = Convert.ToDecimal(TempData["test"]) - validCode.Discount; 
        return Json(new { name = discount }); 
       } 
       //" Valid Code " + " Total Discount : " + validCode.Discount + " Discount Type : (£) " + " Final Price : " + 

      } 
      return Json(new { name = "" }); 
     } 
     else 
     { 
      return Json(new { name = "Invalid Code Entered" }); 
     } 

    } 
+0

那什麼不起作用? –

+0

你在哪裏設置'TempData [「test」]',以及什麼? –

+0

好吧我想用視圖中的結果替換ViewBag.TotalAmount –

回答

0

A先生,

您需要爲您的金額字段添加一個ID。沿着以下線的東西可能工作:

<div class="totalCost"> 
    <label><b>Amount:</b></label><p id="newTotal"><!--existing stuff--></p> <br /> 
</div> 

(你可能甚至不需要上面,如果你使用的<p><br />

和jQuery代碼:

success: function (result) { 
     $('#newTotal').val(result.name); 
} 

這將是我想嘗試的第一件事。

+0

thnx jim dat沒有解決整個問題,但導致我朝着方向.. :) –

+0

很酷作爲mince .. :-) –

相關問題