这是我的控制器。我有一个问题,我不确定为什么我的输入字符串始终为空。希望有人可以看看它,看看我的错误是什么。
var Name;
$("#AccountCodeName").change(function() {
Name = $("#AccountCodeName").val();
});
var form_data = {
"Input": Name,
};
$("#AccountCodeName").autocomplete({
source: function(request, response) {
$.ajax({
url: "/Configuration/AccountCodes/GetAllByName",
method: "POST",
data: form_data,
contentType: "application/json",
success: function(result) {
console.log(result)
}
});
}
});
[HttpPost]
public JsonResult GetAllByName([FromBody]string Input)
{
JsonResult result = new JsonResult(null);
result = this.Json(new
{
list = accountCodeData.GetAllByName(Input),
});
return result;
}
问题是因为您只在页面加载时设置了form_data.Name
。永远不会更新。请注意,它不是参考值。
要解决您需要的问题,请在发送AJAX请求之前创建您提供给data
的对象,如下所示:
$("#AccountCodeName").autocomplete({
source: function(request, response) {
$.ajax({
url: "/Configuration/AccountCodes/GetAllByName",
method: "POST",
data: {
Input: $("#AccountCodeName").val()
},
contentType: "application/json",
success: function(result) {
console.log(result);
// note that you need to call 'response()' here providing the received data
}
});
}
});
我终于找到了答案
这就是它应该用于自动完成的方式
调节器
[HttpPost]
public ActionResult GetAllByName(string term)
{
JsonResult result = new JsonResult(null);
result = this.Json(new
{
list = accountCodeData.GetAllByName(term),
});
return result;
}
jQuery的
$("#AccountCodeName").autocomplete({
source: function (request, response) {
console.log($("#AccountCodeName").val())
console.log(request.term)
$.ajax({
url: "/Configuration/AccountCodes/GetAllByName",
method: "POST",
data: request,
dataType: 'json',
success: function (result) {
console.log(result)
}
});
}
});
我一直在使用带有.net核心post post方法的jquery post甚至模型
以下是可能对您有帮助的代码段
jQuery的
$.ajax({
type: "POST",
url: '/controller/action',
data: { Name: $('#name').val()},
success: function (response) {
window.location.href = '/';
}
});
.net核心后端
[HttpPost, Route("controller/action")]
public async Task<IActionResult> action(string Name)
{
}