I get a number of items through the web service and display them to the user in the form of Json through Jquery
, the problem here is that I get the desired number of items very quickly from the desired web service, but when I want the desired items show it to the user, it takes a long time to render it.The number of desired items is 100 items.
Response Class:
public class ApiResponse
{
public List<ApiQuestion> Questions { get; set; }
public string RenderToHtml { get; set; }
public string Message { get;set; }
}
public class ApiQuestion
{
public int Row { get; set; }
public string Text { get; set; }
}
Method for Get Item:
[HttpPost]
public async Task<ActionResult> PartialQuestionDetail(ApiGetQuestionDetailRequest request)
{
try
{
//**The desired result is taken from the web service and put in the **result** variable**//
//The display speed in this section is low
result.RenderToHtml = WebController.RenderRazorPartialViewToString(
WebController.CreateController<DashboardPartialController>().ControllerContext,
"~/_PartialQuestionDetail.cshtml",
result.Questions);
result.Message = "OK";
return Json(result);
}
catch (Exception ex)
{
return Json(new PostQuestionDetail
{
Message = "ERROR"
});
}
}
Function RenderRazorPartialViewToString
public static string RenderRazorPartialViewToString(ControllerContext context, string fullPathPartialView,
object model = null, bool partial = true)
{
// first find the ViewEngine for this view
ViewEngineResult viewEngineResult = null;
if (partial)
viewEngineResult = ViewEngines.Engines.FindPartialView(context, fullPathPartialView);
else
viewEngineResult = ViewEngines.Engines.FindView(context, fullPathPartialView, null);
if (viewEngineResult == null)
throw new FileNotFoundException("View cannot be found.");
// get the view and attach the model to view data
var view = viewEngineResult.View;
context.Controller.ViewData.Model = model;
string result = null;
using (var sw = new StringWriter())
{
var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
view.Render(ctx, sw);
result = sw.ToString();
}
return result;
}
Function CreateController:
public static T CreateController<T>(RouteData routeData = null) where T : Controller, new()
{
// create a disconnected controller instance
T controller = new T();
// get context wrapper from HttpContext if available
HttpContextBase wrapper;
if (System.Web.HttpContext.Current != null)
wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
else
throw new InvalidOperationException(
"Cannot create Controller Context if no active HttpContext instance is available.");
if (routeData == null)
routeData = new RouteData();
// add the controller routing if not existing
if (!routeData.Values.ContainsKey("controller") &&
!routeData.Values.ContainsKey("Controller"))
routeData.Values.Add("controller", controller.GetType().Name.ToLower().Replace("controller", ""));
controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
return controller;
}
_PartialQuestionDetail.csHtml:
<table class="table custom-table m-0">
<thead>
@{
@*The number of desired items is 100 items.*@
foreach (var item in Model.Response.Questions ?? new List<ApiQuestion>())
{
<tr>
<th>
<label>@Html.Raw(item.Text)</label>
</th>
</tr>
}
</thead>
</table>
Jquery:
function loadQuestionDetail() {
const formData = new FormData();
formData.append('request.QuestionSubjectId', ****);
var xmlHttpRequest = new XMLHttpRequest();
if (!window.XMLHttpRequest) {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpRequest.open(
"POST",
'@Url.Action("PartialQuestionDetail", "***", new {area = "***"})',
true);
xmlHttpRequest.onerror = function() {
window.$('#layoutMain').html(ShowLayoutError(ERROR_TEXT));
};
xmlHttpRequest.onloadend = function() {
var response = ParseJson(xmlHttpRequest.responseText);
switch (response.Message) {
case "ERROR":
window.$('#layoutMain').html(ShowLayoutError(response.Message));
return;
}
window.$('#layoutMain').html(response.RenderToHtml);
}
xmlHttpRequest.send(formData);
}
In general, the problem here is that the information is obtained very quickly through the web service, but when it goes to the Render section, it takes a long time to display the desired items to the user, for this, Json is used, which contains other data along with the desired item I also want to receive it, that's why it is done in Json in the server part