[英]Loading partial view into div only works once
I have the following controller and view: 我有以下控制器并查看:
SearchController.cs : SearchController.cs :
public class SearchController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult SearchResults()
{
int rnd = new Random().Next(100);
var model = new List<SearchResultModel>
{
new SearchResultModel { Id=rnd, FirstName="Peter" + rnd, Surname="Pan" },
new SearchResultModel { Id=rnd+1, FirstName="Jane", Surname="Doe"+rnd }
};
return PartialView("SearchResults", model);
}
}
Index.cshtml : Index.cshtml :
@model WebApplication1.Models.SearchCriterionModel
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
$(document).ready(function () {
$('#btnSearch').click(function (evt) {
evt.preventDefault();
$('#placeholder').load("/Search/SearchResults");
})
});
</script>
<button id="btnSearch">Search</button>
<div id="placeholder"></div>
I just figured out that the template I used for the view (the standard Edit template) inserted a Html.BeginForm
which somehow didn't let me fill a partial view into my div
. 我只是发现我用于视图的模板(标准的Edit模板)插入了
Html.BeginForm
,以某种方式不允许我将局部视图填充到div
。 So I removed the clutter and tried 'from scratch' and look - it worked. 因此,我消除了混乱,并尝试了“从头开始”,然后看-确实可行。 I can now successfully load the partial view
SearchResults
into the div
with id = placeholder
. 现在,我可以使用
id = placeholder
将局部视图SearchResults
成功加载到div
。
But: this only works once. 但是:这仅适用一次。 That is, on the first button click, the code-behind method
SearchResults()
is called and the div
is filled with the first set of 'randomized' data. 也就是说,在第一次单击按钮时,将调用代码隐藏方法
SearchResults()
,并且div
中将填充第一组“随机”数据。 Clicking more than once does enter the client-side click method, but doesn't go into my SearchResults()
method anymore, so I guess no post-back is happening!? 单击不止一次会进入客户端click方法,但是不再进入我的
SearchResults()
方法,所以我想没有回发发生了! Why is that so? 为什么会这样? And how can I 'fix' this, that is, learn how to do it right and get new data everytime I press the button?
我该如何“修复”此问题,也就是说,学会如何正确执行此操作并在每次按下按钮时获取新数据?
My MVC is a bit rusty, but you could try adding a random number to the URL so the browser won't get it from cache. 我的MVC有点生锈,但是您可以尝试向URL添加一个随机数,这样浏览器就不会从缓存中获取它。 Not sure if the controller method will ignore the 'r' parameter or not.
不知道控制器方法是否会忽略'r'参数。
<script type="text/javascript">
$(document).ready(function () {
$('#btnSearch').click(function (evt) {
evt.preventDefault();
var random = getRandomNumber(); //TODO: implement
$('#placeholder').load("/Search/SearchResults?r=" + random);
})
});
</script>
For completion's sake, this is how I ended up doing it. 出于完成的目的,这就是我最终这样做的方式。
First, I used a form because I wanted to pass parameters to the action method in the controller, similar to this: 首先,我使用了一个表单,因为我想将参数传递给控制器中的action方法,类似于:
@using(Html.BeginForm())
{
.
.
some input elements
.
.
<input type="submit" value="Search" />
}
Then I used the following script. 然后,我使用了以下脚本。 Note that for this to work the action method now has to take an instance of the ViewModel as parameter as well.
请注意,要使此方法起作用,现在,动作方法还必须将ViewModel的实例作为参数。
<script type="text/javascript">
$(document).ready(function () {
$("form").submit(function () {
var model = $(this).serialize();
$.ajax({
url: '@Url.Action("SearchResults", "Search")',
data: model,
cache: false,
dataType: "html",
success: function (response) {
$("#placeholder").html(response);
}
});
return false;
})
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.