错误在MVC中的Ajax和交易 [英] Error with the ajax and transaction in mvc

查看:122
本文介绍了错误在MVC中的Ajax和交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Ajax是:

<script>
  $( document ).ready(function(){
    $('#addtocart').click(function () {
      var size = $('#ddlsize').val();
      var color = $('#ddlcolor').val();
      var id ='@Model.ProductId';
      alert(size + color +id);
      $.ajax({
        url: '@Url.Action("AddTocart", "ShoppingCart")',
        data: {                                             
          id:  id,
          size: size,
          color: color,                                           
        },
        dataType: "html",
        type: 'POST',
        success: function (data) {
          alert("Da them vao gio hang");
        },
        error: function () {
          alert("Co loi xay ra vui long thu lai");
        }
      });
    });
  });
</script>

在我的控制器

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)
{
  Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
  var cart = ShoppingCart.Getcart(this.HttpContext);
  cart.AddtoCart(productitem, size, color);
  return View();
}

没有HTTPGET addtocart.When我点击按钮addtocart,在阿贾克斯时的一些错误,但它做的动作addtocart并保存在数据库中,一段时间的成功和一定的时间错误,但犯规保存的数据库,我不知道是什么问题happend?

without the httpget addtocart.When i click button addtocart,some time error in ajax,but it do the action addtocart and save in database,some time success and some time error but doesnt save database,i dont know what problem happend?

推荐答案

由于这种语法

url: '@Url.Action("AddTocart", "ShoppingCart")'

网​​址 Ajax调用的选择是 /我的购物/ AddTocart ,既然你叫返回查看(); 在你的控制器code

The url option of the ajax call would be /ShoppingCart/AddTocart, and since you call return View(); in your controller code

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)
{
    Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
    var cart = ShoppingCart.Getcart(this.HttpContext);
    cart.AddtoCart(productitem, size, color);
    return View();
}

你告诉控制器插入到数据库后打开这个网址: /我的购物/ AddTocart ,我猜想不存在,所以你得到的错误,因为你不必 /Views/ShoppingCart/AddTocart.cshtml 或你没有没有这个方法[HttpPost] ShoppingCartController 类。

You're telling the controller to open this url after inserting to database: /ShoppingCart/AddTocart, which I would guess doesn't exist so you get the error because you don't have /Views/ShoppingCart/AddTocart.cshtml or you don't have this method without [HttpPost] attribute in ShoppingCartController class.

public ActionResult AddTocart()
{
}

您应该返回JSON如下:

You should return json as follows

[HttpPost]
public ActionResult AddTocart(int id, string size, string color)
{
    Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
    var cart = ShoppingCart.Getcart(this.HttpContext);
    cart.AddtoCart(productitem, size, color);
    return Json(new { success = true });
}

和变化的dataType 选项 JSON

<script>
  $( document ).ready(function(){
    $('#addtocart').click(function () {
      var size = $('#ddlsize').val();
      var color = $('#ddlcolor').val();
      var id ='@Model.ProductId';
      alert(size + color +id);
      $.ajax({
        url: '@Url.Action("AddTocart", "ShoppingCart")',
        data: {                                             
          id:  id,
          size: size,
          color: color,                                           
        },
        dataType: "json",
        type: 'POST',
        success: function (data) {
          alert("Da them vao gio hang");
        },
        error: function () {
          alert("Co loi xay ra vui long thu lai");
        }
      });
    });
  });
</script>

这篇关于错误在MVC中的Ajax和交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆