MVC前台Post/Get异步获得数据时参数的取值问题
Post方法,返回text,后台获得Data
View
$.ajax({
type: "POST",
dataType: "text",//返回类型为文本
url:"/Order/AjaxGetCoupon?ids="+id,
data: { ConponCode: code, TotalPrice: totalPrice },
success: function (data) {
if (data == "False") {
ShowFailTip('服务器繁忙,请稍候再试!');
return;
}
else if (data == "No") {
ShowFailTip('您输入的优惠券不存在或已过期!');
return;
} else if (data == "Used") {
ShowFailTip('您输入的优惠券已使用!');
return;
} else if (data == "Limit") {
ShowFailTip('您输入的优惠券不满足最低消费金额!');
return;
} else {
$("#couponPriceId").text("¥" + data.split("|")[0]);
$("#payPriceId").text("¥" + data.split("|")[1]);
}
}
});
Code
[HttpPost]
public ActionResult AjaxGetCoupon(FormCollection Fm,int ids) {//ids为url中的参数或后台获得URL后的参数 Request.QueryString["ids"] if (string.IsNullOrWhiteSpace(Fm["ConponCode"])) { return base.Content("False"); } string couponCode = Fm["ConponCode"]; decimal num = Globals.SafeDecimal(Fm["TotalPrice"], (decimal) 0M); BLL.CouponInfo info = new BLL.CouponInfo(); Model.CouponInfo couponInfo = info.GetCouponInfo(couponCode, false); if (couponInfo != null) { if (couponInfo.Status == 2) { return base.Content("Used"); } if (couponInfo.LimitPrice >= num) { return base.Content("Limit"); } ShoppingCartInfo cartInfo = new ShoppingCartHelper(base.currentUser.UserID).GetShoppingCart4Selected(); switch (info.GetUseStatus(cartInfo, couponInfo)) { case 0: return base.Content("No"); case 1: { string str2 = (num - couponInfo.CouponPrice).ToString("F"); return base.Content(couponInfo.CouponPrice.ToString("F") + "|" + str2); } case 2: return base.Content("Used"); case 3: return base.Content("Limit"); case 4: return base.Content("CategoryLimit"); case 5: return base.Content("ProductLimit"); case 6: return base.Content("SKULimit"); case 7: return base.Content("CategoryNo"); case 8: return base.Content("ProductNo"); case 9: return base.Content("SKUNo"); } } return base.Content("No"); }
若需要返回json则将 $.ajax 中 dataType: "json"
后台:public JsonResult AjaxGetCoupon(FormCollection Fm,int ids){
return Json(new { errCode =0,message= "非常抱歉" });
}
页面传参
location.href = " Order/SubmitOrder?sku=" + sku + "&g=" + groupbuyid;
public ActionResult SubmitOrder(string sku, int count = 1, int c = 0, int g = 0, string viewName = "SubmitOrder")
{
ShoppingCartInfo cartInfo = new ShoppingCartInfo();
return base.View(viewName, cartInfo);
}
作者:欢醉
公众号【一个码农的日常】 技术群:319931204 1号群: 437802986 2号群: 340250479
出处:http://zhangs1986.cnblogs.com/
码云:https://gitee.com/huanzui
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
公众号【一个码农的日常】 技术群:319931204 1号群: 437802986 2号群: 340250479
出处:http://zhangs1986.cnblogs.com/
码云:https://gitee.com/huanzui
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2013-11-28 JS代码格式化和语法着色