【发布时间】:2015-03-09 13:50:48
【问题描述】:
每当select-字段发生变化时,我都会尝试从数据库中的表中获取一些信息。
我能够做出响应,并且 Ajax jQuery 函数返回成功(函数)
问题是我不知道如何使用 ASP.net MVC 从我的数据库返回数据。
我从我的 Ajax 成功调用中得到的响应是这样的文本:
ProduktWebKort.Models.WarehouseCustomer[]
这是我的控制器的一部分,带有通过 Ajax 调用的函数
[HttpPost]
public string OnChange(int id)
{
return db.Customers.Where(C => C.CustomerID == id).ToString();
}
我的 Ajax 调用如下所示:
<script type="text/javascript">
$(function () {
// Document.ready -> link up remove event handler
$("#CustomerID").change(function () {
alert("Hej");
var option = $('option:selected', this).val();
$.ajax({
type: "POST",
url: '@Url.Action("Index/1", "Session")',
contentType: "application/json; charset=utf-8",
data: { id: 1 },
dataType: "html",
success: function (success) {
alert('Success');
console.log(success);
for (var i = 0; i < success.length; i++) {
$(".form-horizontal").append("<li>" + success[i] + " </li>");
console.log(success);
}
},
error: function (e, text, string) {
alert(e.message + text + string);
console.log(e);
}
});
});
});
</script>
我想返回来自WarehouseCustomer 的每个条目,并从我的选择字段中返回ID 为CustomerID。
【问题讨论】:
-
通常为了响应 MVC 5 应用程序中的 AJAX 调用,您实际上会使用 Web API。当前函数返回什么,你希望它返回什么?
-
你想做什么?获取一位具有特定 ID 的客户?或者获取客户列表?
-
您真正在寻找什么?如果您只是在寻找建议,那么正如@mason 所说,您应该使用
Web API并返回一些 JSON 以将其与您的下拉菜单绑定,如果您有任何其他问题,请更新您的问题。 -
它当前返回如下字符串: ProduktWebKort.Models.WarehouseCustomer[] 我正在尝试返回所有 WarehouseCustomers 的列表,其 CustomerID 由 select 选择
-
@SørenFritzbøger 您应该编辑您的问题以包含结果。对问题至关重要的信息(例如预期输出和实际输出)是问题的重要组成部分,因此属于问题本身而不是 cmets。
标签: jquery asp.net ajax asp.net-mvc