After looking up related StackOverflow topics I did not find any appropriate solution I finally come up to post... initially: ajax works, I do have a server response of the html filled up table partialview, however it is not rendered at the specified of which id is "QuerySection". scripts bundled: "~/Scripts/jquery-1.9.1.js", "~/Scripts/MicrosoftAjax.js", "~/Scripts/MicrosoftMvcAjax.js" as well as jquery.unobtrusive-ajax.js
what am I doing wrong?
controller's action partialview added filename below
[HttpPost]
public ActionResult LogsPartial(string[] choices)
{
var datebegin = choices[0].Length==0?new DateTime(2014,1,1): DateTime.Parse(choices[0]);
var dateend = choices[1].Length == 0 ? new DateTime(2150, 1, 1) : DateTime.Parse(choices [1]);
var lstLogs = db.Logs.Include("Users").Where(x => x.DateOfAction >= datebegin &&.DateOfAction <= dateend).ToList();
if (lstLogs.Any())
{
return PartialView("LogsPartial", lstLogs);
}
else
return PartialView("LogsPartial",null);
}
partialview >> LogsPartial.cshtml
@model IEnumerable<ContiRFQ.Models.Logs>
@if (Model == null || Model.Count() == 0)
{
<p>No results found</p>
}
else
{
<h2>Logs</h2>
<table>
<tr>
<th>
Account
</th>
<th>
Action
</th>
<th>
Date of Action
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Users.Account)
</td>
<td>
@Html.DisplayFor(modelItem => item.Action)
</td>
<td>
@item.DateOfAction.ToString("MMMM dd yyyy, HH:mm")
</td>
</tr>
}
</table>
}
and the index.cshtml
<script type="text/javascript">
$(document).ready(function () {
$('#submit').click(function () {
var dates = [$('#BeginDate').val(), $('#EndDate').val()];
var json = JSON.stringify(dates);
$.ajax({
url: "@Url.Action("LogsPartial", "Logs")",
type: 'POST',
cache: false,
contentType: 'application/json; charset=utf-8',
data: json,
dataType: 'json',
success: function (data)
{
$('#QuerySection').html(data);
},
error: function () {
console.trace();
}
});
});
});
<div id="QuerySection"> </div>
I would try changing the action method to a PartialViewResult (explicitly) instead of an ActionResult. Then you just need to do
return PartialView(lstLogs);
I am guessing that the OS handles it based on the return type specific in the method signature rather than what you actually return.
Thank you all guys, after some hours struggling /trying it turned out that an evident parameter of .ajax function was wrong. datatype:json, though I was expecting html ......
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.