[英]Partial view does not Update with ms ajax in asp.net MVC
I have a search text box and a button.我有一个搜索文本框和一个按钮。 I want to show the results of the search in a partial view with ajax when the button is clicked.
我想在单击按钮时使用 ajax 在部分视图中显示搜索结果。 When I set a break point in the partial view I can see data but nothing shows on the form.
当我在局部视图中设置断点时,我可以看到数据,但表单上没有显示任何内容。
My controller that passes the text value for the search:我为搜索传递文本值的控制器:
[HttpPost]
public async Task<ActionResult> Index(string searchtext)
{
// search data and put it in Results here ...
ViewBag.Results = Results;
return PartialView("_SearchResults");
}
My index.cshtml view code:我的 index.cshtml 查看代码:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<input autofocus="autofocus" class="form-control" id="SearchStr" name="SearchStr" placeholder="search" required="required" title="Search" type="search" value="">
@Html.SubmitButton("Search", "btnSearch")
<div id="resultsDiv">
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "resultsDiv" }))
{
<p>hre</p>
}
</div>
}
@section scripts
{
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
$("#btnSearch").on("click", function (e) {
e.preventDefault();
$.ajax({
type: "post",
datatype: "json",
url: "/Home/Index",
data: {
searchtext: document.getElementById("SearchStr").value
}
});
});
</script>
}
and this is my partial view that in brek point I can see 51 number of results ..这是我的部分观点,在 brek 点我可以看到 51 个结果..
@if (ViewBag.Results != null)
{
foreach (var person in ViewBag.Results.Data)
{
<div>
<p>
@person.FullName
</p>
</div>
}
}
I don't have an error in compile and run time, and no error in the browser ...我在编译和运行时没有错误,浏览器也没有错误......
I learned this step by step with this video: video我通过这个视频一步一步地学会了这一点:视频
Try This One试试这个
<div id="resultsDiv"> </div>
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "resultsDiv" }))
{
<p>hre</p>
}
Also make sure your foreach loop data还要确保您的 foreach 循环数据
foreach (var person in ViewBag.Results.Data)
{
<div>
<p>
@person.FullName
</p>
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.