2015-07-21 30 views
0

我在部分視圖中使用Jquery將數據傳遞給控制器​​。這裏是我的局部視圖代碼:在部分視圖中獲取Jquery中的空值

@model List<Apostille.Models.Cart> 

@{ 
    var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10); 
    grid.Pager(WebGridPagerModes.All); 
} 

@using (Html.BeginForm("MinusQuantity", "Cart", FormMethod.Post, new { id = "myForm" })) 
{ 
    <div> 
     @grid.GetHtml(
        tableStyle: "webgrid-table", 
        headerStyle: "webgrid-header", 
        footerStyle: "webgrid-footer", 
        alternatingRowStyle: "webgrid-alternating-row", 
        selectedRowStyle: "webgrid-selected-row", 
        rowStyle: "webgrid-row-style", 
        mode: WebGridPagerModes.All, 
    columns: grid.Columns(
      grid.Column(columnName: "country_name", header: "Country Name"), 
      grid.Column(columnName: "state_name", header: "State Name"), 
      grid.Column(columnName: "document_title", header: "Document Title"), 
      grid.Column(columnName: "document_name", header: "Document Name"), 
      grid.Column(header: "Document Quantity", format: @<div><button onclick="minus(@item.order_id,@item.number_of_document)" data-type="minus" class="btn btn-danger btn-number" data-field="number_of_document"><b>-</b></button> <span style="margin-left: 18%">@Html.Raw(@item.number_of_document)</span> <button onclick="plus(@item.order_id,@item.number_of_document)" data-type="plus" class="btn btn-success btn-number" data-field="number_of_document", style="float:right"><b>+</b></button></div>), 
      grid.Column(columnName: "first_doc_price", header: "First Doc Price"), 
      grid.Column(columnName: "addition_doc_price", header: "Additional Doc Price"), 
      grid.Column(columnName: "extra_total", header: "Addtional Doc Total"), 
      grid.Column(columnName: "total", header: "Final Total") 
      //grid.Column(format: (item) => Html.ActionLink("Edit", "_EditCartPartial", new { @id = "send id here" })) 
    )) 
    </div> 
} 

<script type="text/javascript"> 

    jQuery(document).ready(function() { 
     alert("DOM loaded!"); 

     function minus(orderId, qty) { 
      $.ajax({ 
       url: '@Url.Action("MinusQuantity", "Cart")', 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: { 'OrderId': orderId, 'Quantity': qty }, 
       sucess: function (data) { 

       } 
      }) 
     } 
    }); 
    //alert("this script tag is executing properly!"); 

    function plus(orderId, qty) { 
     $.ajax({ 
      url: '@Url.Action("PlusQuantity", "Cart")', 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      data: { 'OrderId': orderId, 'Quantity': qty }, 
      sucess: function (data) { 

      } 
     }) 
    } 
</script> 

我jQuery的成功調用,但我得到的訂單ID和數量空值,當我點擊按鈕的WebGrid。

我讀了jQuery不能在部分視圖中工作的地方,所以我試圖把jquery放在index.cshtml中,但是我仍然得到了null值。

但是,如果我將webgrid移動到index.html,jquery工作都很好。

可能的原因是什麼,jQuery不能在局部視圖中工作?我該怎麼做才能克服這種情況?

回答

0

使用parseInt函數

function minus(orderId, qty) { 
      var ordId = parseInt(orderId); 
      var quant = parseInt(qty); 
      $.ajax({ 
       url: '@Url.Action("MinusQuantity", "Cart")', 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: { 'OrderId': ordId , 'Quantity': quant}, 
       sucess: function (data) { 

       } 
      }) 
     } 
+0

謝謝你的答案 –

相關問題