我是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);
}
当我在Controller中使用JsonResult函数时,我只会收到“成功”警报,但是我需要它最终将LINQ查询返回给View();函数,并且在使用JsonResult函数时不起作用。任何想法都会有所帮助。如果有更好的方法,我不必使用ajax。我只需要一种将userCounty中存储的字符串传递给控制器的方法。
请像这样更改您的控制器方法
[HttpPost]
public ActionResult Index([FromBody]string userCounty)
{
var query = //use linq to query database
return View(query);
}
要查看页面,您需要
[HttpGet]
public ActionResult Index()
{
return View();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句