/// <summary> /// /// </summary> /// <param name="elementId">Oluşacak div elementinin id si</param> /// <param name="controller">Verinin çekileceği controller adı</param> /// <param name="action">Verinin çekileceği action adı</param> /// <param name="queryString">Action a gönderilecek querystring ler</param> /// <param name="previewPartialName">Loading in gösterileceği partial adı</param> /// <param name="method">İstek tipi</param> /// <param name="tempData">Harici tempdata ile gönderilecek script</param> /// <returns></returns> public static MvcHtmlString AsyncPartial(this HtmlHelper helper, string elementId, string controller, string action, string queryString = "", string previewPartialName = null, AjaxMethods method = AjaxMethods.GET, TempDataDictionary tempData = null) { var xo = $"x_{elementId}"; var js = $"function get_{elementId}()" + "{" + $"var asyncElement= document.getElementById('div-{elementId}'); " + $"var asyncElementLoading= document.getElementById('div-{elementId}-loading'); " + "var " + xo + " = new XMLHttpRequest();" + xo + ".onreadystatechange = function() {" + " if (" + xo + ".readyState == XMLHttpRequest.DONE ) {" + " if (" + xo + ".status == 200) {" + $"asyncElement.innerHTML = " + xo + $".responseText.replace(/data-partial-refresh/ig, 'data-partial-refresh onclick=\"get_{elementId}()\"');" + $"asyncElementLoading.style.display='none';" + $"asyncElement.style.display='block';" + " }" + "else if (" + xo + ".status == 400) {" + " alert('Error 400');" + " }" + "else{" + " alert('Generic error');console.log(x_test1);" + " }" + " }" + "};" + $"asyncElementLoading.style.display='block';" + $"asyncElement.style.display='none';" + xo + $".open('{method}', '/{controller}/{action}{queryString}', true);" + xo + ".send();" + "};" + $"get_{elementId}();"; var stringPartial = helper.Partial(previewPartialName ?? "Global_preview"); if (tempData != null) { tempData["script"] += js; return(MvcHtmlString.Create($"<div id='div-{elementId}'>" + stringPartial + "</div>" + $"<div id='div-{elementId}-loading' style='display:none'>" + stringPartial + "</div>" )); } else { return(MvcHtmlString.Create($"<div id='div-{elementId}'>" + stringPartial + "</div>" + $"<div id='div-{elementId}-loading' style='display:none'>" + stringPartial + "</div>" + "<script>" + js + "</script>")); } }
public static MvcHtmlString AsyncPartial(this HtmlHelper helper, string controller, string action, string previewPartialName = null, AjaxMethods method = AjaxMethods.GET, TempDataDictionary TempData = null) { string xo = $"x_{controller}_{action}"; string js = $"function get_{controller}_{action}()" + "{" + $"var d= document.getElementById('div-{controller}-{action}'); " + $"var ds= document.getElementById('div-{controller}-{action}-sh'); " + "var " + xo + " = new XMLHttpRequest();" + xo + ".onreadystatechange = function() {" + " if (" + xo + ".readyState == XMLHttpRequest.DONE ) {" + " if (" + xo + ".status == 200) {" + $"d.innerHTML = " + xo + ".responseText.replace(/data-partial-refresh/ig, 'data-partial-refresh onclick=\"get_{controller}_{action}()\"');" + $"ds.style.display='none';" + $"d.style.display='block';" + " }" + "else if (" + xo + ".status == 400) {" + " alert('Error 400');" + " }" + "else{" + " alert('Generic error');" + " }" + " }" + "};" + $"ds.style.display='block';" + $"d.style.display='none';" + xo + $".open('{method}', '/{controller}/{action}', true);" + xo + ".send();" + "};" + $"get_{controller}_{action}();"; MvcHtmlString StringPartial = PartialExtensions.Partial(helper, previewPartialName == null ? "Preview" : previewPartialName); if (TempData != null) { TempData["script"] += js; return(MvcHtmlString.Create($"<div id='div-{controller}-{action}'>" + StringPartial.ToString() + "</div>" + $"<div id='div-{controller}-{action}-sh' style='display:none'>" + StringPartial.ToString() + "</div>" )); } else { return(MvcHtmlString.Create($"<div id='div-{controller}-{action}'>" + StringPartial.ToString() + "</div>" + $"<div id='div-{controller}-{action}-sh' style='display:none'>" + StringPartial.ToString() + "</div>" + "<script>" + js + "</script>")); } }