【发布时间】:2021-03-21 03:11:27
【问题描述】:
我是 ASP 新手,我正在尝试在我的 JS 代码中获取一个字符串并将其发布到我的控制器,以便我可以使用它来查询我的数据库。
JavaScript
function findEmployees(userCounty) {
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("Index", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
success: function (response) {
alert(userCounty);
},
error: function (response) {
alert("failed");
}
});
}
控制器
[HttpPost]
public ActionResult Index (string userCounty)
{
var query = //use linq to query database
return View(query);
}
当我在控制器中使用 JsonResult 函数时,我只会收到“成功”警报,但我需要它最终将 LINQ 查询返回给 View();函数,并且在使用 JsonResult 函数时不起作用。任何想法都会有所帮助。如果有更好的方法,我不一定必须使用 ajax。我只需要将存储在 userCounty 中的字符串传递给我的控制器。
【问题讨论】:
标签: javascript c# asp.net ajax asp.net-core