I have the following controller and view:
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:
@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
. 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
.
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. 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!?
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?