这是我的控制器。我有一个问题,我不确定为什么我的输入字符串始终为空。希望有人可以看看它,看看我的错误是什么。
config.middleware.use Rack::Session::Cookie, secret: "s3cr3t_k3y_3x@mpl3"
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)
}
});
}
});
答案 0 :(得分:1)
问题是因为只有在页面加载时才设置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
}
});
}
});
答案 1 :(得分:0)
我终于找到了答案
这应该是自动完成的方式
控制器
[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)
}
});
}
});
答案 2 :(得分:0)
我一直在将jquery post与.net核心post action方法用于甚至模型
以下代码段可能会对您有所帮助
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)
{
}