按钮单击参数不会传递给控制器

时间:2013-02-11 07:23:52

标签: asp.net-mvc

我点击按钮,但参数没有传递到我的视图。按钮将正常工作,我刚刚把它放在警报消息上将显示警告消息。     我的观点如下:

       <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui-1.8.20.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui-1.8.20.min.js" type="text/javascript"/>               
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                   $('#AddToCard').click(function () {
                    var ProductID = $('#txtproductID').val();
                    var Quantity = $('#txtQuantity').val();
                    var NetAmt = $('#txtNetAmt').val();
                    var ProName = $('#txtPN').val();
                    alert("ID="+ProductID+"QTY=" + Quantity+"NETAMT=" + NetAmt+"Name=" + ProName);
                    $.ajax({
                        type: "POST",
                        url: '../Home/AddOrderItem',
                       data: { productCode: ProductID, quantity: Quantity, NAMT: NetAmt, PName: ProName },

                        success: function (data) {
                             if (data) {
        //                        alert("Added To your Card");
                            }
                      }
                    });
                });


            });
        </script>

我的cotroller

       public void AddOrderItem(Int64 productCode, int quantity, decimal NAMT, string PName)
            {
                //adds an item to the current order
                OrderDO order = (OrderDO)Session["Order"];
                decimal TotAmt = quantity * NAMT;
                order.Items.Add(new OrderItem(productCode, quantity,NAMT,PName,TotAmt));
                Session["Order"] = order;
              }

1 个答案:

答案 0 :(得分:0)

要将控制器的结果返回到View,您应该返回Json对象。

public ActionResult AddOrderItem(Int64 productCode, int quantity, decimal NAMT, string PName)
{
    //adds an item to the current order
    OrderDO order = (OrderDO)Session["Order"];
    decimal TotAmt = quantity * NAMT;
    order.Items.Add(new OrderItem(productCode, quantity,NAMT,PName,TotAmt));
    Session["Order"] = order;

    return Json(order, JsonRequestBehavior.AllowGet);
}