C#MVC触发成功“返回JSON”
问题描述:
之前,我有一个脚本(包括以下),其在下拉列表的变化,以及事件处理火灾应填写值两个文本输入的视图。值从控制器接收。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);
}
答
发送一列表,其数据值可以使用访问
success: function(data)
{
$("#txtQuantity").val(data[0].Quantity);
$("#txtPrice").val(data[0].Price);
}
对不起!这是什么意思 ? **只要itemstatus列表填充控制器返回查看和没有值传回给Ajax函数**? – Shyju
对不起,还不够清楚。 所以,一旦执行此行: 列表- itemstatus = (从PD在db.Item 其中(pd.itemID == ID) 选择新的项目(){ ITEMPRICE = pd.itemPrice, itemQuantity = pd.itemQuantity })。ToList(); 控制器返回查看。 –
user3809390
这是我不明白的部分**控制器返回查看**?你怎么知道 ?该方法通过当前视图中的ajax调用执行。因此,在代码执行过程中,您仍然可以在浏览器中看到当前视图。那么你的控制器返回的意思是什么?哪个视图?你已经看到了一个观点。 – Shyju