【发布时间】:2017-02-20 19:32:52
【问题描述】:
我正在尝试根据查询过滤数据。我正在使用部分视图来显示过滤后的数据。但是,局部视图中没有任何更新。我该如何解决这个问题?
控制器类:
public IActionResult Index() {
IEnumerable<Organisation> organisations = _organisationRepository.GetAll().OrderBy(b => b.Name).ToList();
if (isAjaxRequest()) {
string text = Request.Form["text"].ToString();
return PartialView("_Organisations", _organisationRepository.GetByName(text).ToList());
}
return View(organisations);
}
private bool isAjaxRequest() {
return Request != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest";
}
组织视图:
<form asp-controller="Organisation" asp-action="Index">
<div class="form-inline">
<div class="form-group">
<label for="text">Filteren in organisaties</label>
<input type="text" name="text" id="text" required pattern="^([0-9]{4}|[a-zA-Z]+)$">
<label for="selected"></label>
<select name="selected" id="selected" class="form-control">
<option value="name">Naam</option>
<option value="postalcode">Postcode</option>
</select>
</div>
<button type="submit" class="btn btn-default" id="search">Zoeken</button>
</div>
</form>
<div id="partial">
@Html.Partial("_Organisations", Model);
</div>
局部视图:
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>Naam</th>
<th>Adres</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.Name
</td>
<td>
@item.Location.ToString()
</td>
<td>
<a onClick="showForm(this, @item.OrganisationId)" href="#">Selecteer</a>
</td>
</tr>
}
Javascript 文件:
$("#search").click(function () {
$.ajax({
url: "Organisation/Index",
type: "post",
data: $("form").serialize(),
success: function (result) {
$("#partial").html(result);
}
});
});
任何帮助将不胜感激!
【问题讨论】:
标签: c# .net ajax forms asp.net-core-mvc