所以我有一个字符串从 JS 传递到我的控制器,如下所示:
JavaScript
function findEmployees(userCounty) {
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("getCounty", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
});
}
控制器
[HttpPost]
public ActionResult Index([FromBody] string userCounty)
{
var county = userCounty.Substring(0, userCounty.IndexOf(" "));
var query = from m in _context.model where m.county == county select new Model
{
FirstName = m.Firstname
LastName = m.LastName
};
if (query == null)
{
return NotFound();
}
return View(query.ToList());
}
[HttpGet]
public ActionResult Index()
{
return View();
}
看法
@model Project.Models.ModelName
<table class="table">
<tbody>
<tr>
<td>
@Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)
</td>
</tr>
</tbody>
我可以将字符串从 JS 传递到我的控制器并查询数据库,但如何更新页面以在我的视图中显示查询结果?任何事情都有帮助。谢谢你!
慕工程0101907
猛跑小猪
相关分类