2017-01-01 57 views
0

之前,我有一個腳本(包括以下),其在下拉列表的變化,以及事件處理火災應填寫值兩個文本輸入的視圖。值從控制器接收。C#MVC觸發成功「返回JSON」

我遇到的問題是,只要itemstatus列表被填充,控制器就會返回查看,並且沒有值被傳回給AJAX函數。

如果我刪除itemstatus列表代碼和手動如下賦值,它的工作原理:

var result = new { Quantity = "123", Price = "555"}; 

然後它工作。

我也嘗試過其他方法來控制內獲得的數據,但結果都是一樣的。你有什麼想法什麼我做錯了,爲什麼控制器返回之前的 「返回JSON」

<script type="text/javascript"> 

$(document).ready(function() { 
    //Dropdownlist Selectedchange event 
    $("#dropdown1").change(function() 
    { 
     $("#txtQuantity").empty(); 
     $("#txtPrice").empty(); 
     $.ajax(
     { 
      type: 'POST', 
      url: '@Url.Action("GetValues")', 
      dataType: 'json', 
      data: { id: $("#dropdown1").val() }, 
      success: function(data) 
      { 
       $("#txtQuantity").val(data.Quantity); 
       $("#txtPrice").val(data.Price); 
      } 
     }) 
    }) 
}); 

控制器查看:因爲你是

public JsonResult GetValues(int id) 
    { 

     List<Item> itemstatus = 
      (from pd in db.Item 

      where (pd.itemID == id) 
      select new Item() 
      { 
       itemPrice = pd.itemPrice, 
       itemQuantity = pd.itemQuantity 
      }).ToList(); 

     ... 
     more code where i select what i need as a result i have two strings named itemquantity and itemprice 
     ... 

     var result = new { Quantity= itemquantity, Price = itemprice }; 
     return Json(result, JsonRequestBehavior.AllowGet); 

}

+0

對不起!這是什麼意思 ? **只要itemstatus列表填充控制器返回查看和沒有值傳回給Ajax函數**? – Shyju

+0

對不起,還不夠清楚。 所以,一旦執行此行: 列表 itemstatus = (從PD在db.Item 其中(pd.itemID == ID) 選擇新的項目(){ ITEMPRICE = pd.itemPrice, itemQuantity = pd.itemQuantity })。ToList(); 控制器返回查看。 – user3809390

+0

這是我不明白的部分**控制器返回查看**?你怎麼知道 ?該方法通過當前視圖中的ajax調用執行。因此,在代碼執行過程中,您仍然可以在瀏覽器中看到當前視圖。那麼你的控制器返回的意思是什麼?哪個視圖?你已經看到了一個觀點。 – Shyju

回答

0

發送一列表,其數據值可以使用訪問

success: function(data) 
     { 
      $("#txtQuantity").val(data[0].Quantity); 
      $("#txtPrice").val(data[0].Price); 
     }