2 回答

TA貢獻1887條經驗 獲得超5個贊
ajax返回的數據是text或者json。如果你想用c#來更新頁面。你可以讓actiongetCounty返回partial view,partial view自動用html返回數據。
改變行動getCounty。
[HttpPost("getCounty")]
public ActionResult Index([FromBody] string userCounty)
{
var county = userCounty.Substring(0, userCounty.IndexOf(" "));
//...
return PartialView(query.ToList());
}
PartialView索引.cshtml
@model List<ModelName>
<table class="table">
<tbody>
@for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(model => model[i].FirstName) @Html.DisplayFor(model => model[i].LastName)
</td>
</tr>
}
</tbody>
</table>
看法
@model ModelName
<div id="datalist">
</div>
<!--other code-->
@section Scripts{
<script>
function findEmployees(userCounty) {
$.ajax({
type: "POST",
//dataType: "json",
url: '@Url.Action("getCounty", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
success: function (data) {
$('#datalist').html(data)
},
error: function (e) {
console.log(e)
}
});
}
</script>
}
可以根據userCounty生成不同的數據表

TA貢獻1858條經驗 獲得超8個贊
您可以像這樣將列表獲取到頁面。然后您可以在每個循環中按 div 或 ul 列表內部。
function findEmployees(userCounty) {
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("getCounty", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
success: function (result) {
if (result.data.length !== 0) {
$.each(result.data, function (index, value) {
var firstName = value.firstName;
var lastName = value.lastName;
});
}
},
});
}
添加回答
舉報