Asp.net mvc 使用jQuery';s Ajax在mvc2中对视图进行瞬时计算
我有一个total product页面,其中显示了添加到购物篮中的产品总量,我想做的是添加一个促销文本字段,用户可以在其中添加促销代码以获得一定的折扣,促销代码以折扣率保存在数据库中。因此,如果客户输入代码并按下促销按钮,系统应检查有效的促销代码,然后通过折扣率扣除总价进行即时计算。如果这可以使用jqueryajax来完成,或者有任何其他可能的解决方案,任何帮助或教程都将不胜感激。干杯Asp.net mvc 使用jQuery';s Ajax在mvc2中对视图进行瞬时计算,asp.net-mvc,asp.net-mvc-2,jquery,Asp.net Mvc,Asp.net Mvc 2,Jquery,我有一个total product页面,其中显示了添加到购物篮中的产品总量,我想做的是添加一个促销文本字段,用户可以在其中添加促销代码以获得一定的折扣,促销代码以折扣率保存在数据库中。因此,如果客户输入代码并按下促销按钮,系统应检查有效的促销代码,然后通过折扣率扣除总价进行即时计算。如果这可以使用jqueryajax来完成,或者有任何其他可能的解决方案,任何帮助或教程都将不胜感激。干杯 <script language="javascript" type="text/javascript"
<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> }%>
$(文档).ready(函数(){
$(“#促销”)。更改(功能){
var$this=$(this);
var number=$('promoNumber').val();
$.ajax({
url:“/Booking/Review”,
类型:“POST”,
数据:{number:textbox},
成功:功能(数据){
如果(数据[“成功”]){
警报(数据)
}
}
});
});
});
控制器
[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();
}
[HttpPost]
public ActionResult Review(int number)//此参数是我选中的复选框值。我必须从ajax获取此值
{
IVoucherRepository voucherResp=新的VoucherRepository();
IQueryable get凭单=凭证对应的get凭单(编号);
//首先检查代码是否存在,否则返回错误
整数折扣=0;
foreach(getVoucher中的var编号)
{
折扣=数字。折扣;//这是折扣数字
}
//获得折扣代码后,我需要将其传递给ViewBag.TotalAmount进行计算
ViewData[“数据”]=“成功”;
返回视图();
}
将jQuery post事件附加到优惠券按钮:
$('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 */
}
});
});
就像ghayes提到的那样,如果有现有的解决方案,就使用它们。他们很可能考虑了许多您不知道的情况。我发现您将数据发布到控制器时出现了一个问题:
var number = $('promoNumber').val();
然后:
data: {number:textbox},
什么是文本框?您应该在jQuery中这样引用您的促销编号:
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作为返回对象 这是控制器:
[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));
}
[HttpPost]
公共行动结果审查(国际编号)
{
IVoucherRepository voucherResp=新的VoucherRepository();
IQueryable get凭单=凭证对应的get凭单(编号);
//首先检查代码是否存在,否则返回错误
整数折扣=0;
int totalAmount=0;
foreach(getVoucher中的var编号)
{
折扣=数字。折扣;//这是折扣数字
}
//获得折扣代码后,我需要将其传递给ViewBag.TotalAmount进行计算
返回(Json(新的{discount=discount,totalAmount=totalAmount},JsonRequestBehavior.DenyGet));
}
在控制器中,您可以执行所有计算,然后返回一个json对象,其中包含您想要的所有属性。这当然可以通过Javascript/AJAX完成。jQuery将是一个很好的起点。或者,Knockout.js是一个主要基于实现这一目标的框架。您应该从选择一个框架开始,并使用它。你有没有相关的代码来解决这个问题?是的,但是你也在使用jQuery(来自你的标签)。基本上,是的,当用户输入优惠券代码时,您可以发出AJAX请求,该请求将返回相关的价格折扣并适当地更新字段。这是关于具体的答案可以根据您的问题给出,没有任何其他相关的代码。我想避免回发,我可以使用按钮的回发进行计算,但我不想渲染整个页面,只是为了更新总价。是的,AJAX将阻止这一点。代码员,我需要代码来帮助你。抽象地说,这相当简单。但问题在于细节。