Generic ajax POST and GET method in MVC and Jquery

//POST method
var ACTIVE_REQ_CNT = 0;
function fnCallAjaxHttpPostEvent(url, postData, isAsync, showLoader, successCallback) {
 var args = Array.prototype.slice.call(arguments).slice(5);
 return $.ajax({
 async: isAsync,
 type: "POST",
 contentType: "application/json",
 url: url,
 cache: false,
 data: JSON.stringify(postData),
 beforeSend: function () {
 if (showLoader) {
 ACTIVE_REQ_CNT++;
 fnShowWaitImage();
 }
 },
 success: function (data, textStatus, jqXHR) {
 if (fnScanResponseForSessionTimeOut(jqXHR)) {
 fnSessionTimeOutHandler();
 } else {
 var callbackArgs = [];
 callbackArgs.push(data)
 callbackArgs = callbackArgs.concat(args);
 try {
 successCallback.apply(this, callbackArgs);
 } catch (ex) {
 console.error(ex);
 }
 }
 },
 failure: function (response) {
 console.error(response);
 },
 complete: function () {
 if (showLoader) {
 ACTIVE_REQ_CNT--;
 ACTIVE_REQ_CNT === 0 && fnHideWaitImage();
 }
 }
 });
}



//How to use POST method 
fnCallAjaxHttpPostEvent('@Url.Action("ActionName", "ControllerName")', paramModel , true, true, fnSaveSuccess, popupObj);

function fnSaveSuccess(data, popupObj) {
}

Here, popupObj is the extra parameter that can be fetched in successCallback function.



//GET method
var ACTIVE_REQ_CNT = 0;
function fnCallAjaxHttpGetEvent(url, param, isAsync, showLoader, successCallback) {
 var args = Array.prototype.slice.call(arguments).slice(5);
 return $.ajax({
 async: isAsync,
 type: "GET",
 contentType: "application/json",
 url: url,
 cache: false,
 data: param,
 beforeSend: function () {
 if (showLoader) {
 ACTIVE_REQ_CNT++;
 fnShowWaitImage();
 }
 },
 success: function (data, textStatus, jqXHR) {
 if (fnScanResponseForSessionTimeOut(jqXHR)) {
 fnSessionTimeOutHandler();
 } else {
 var callbackArgs = [];
 callbackArgs.push(data)
 callbackArgs = callbackArgs.concat(args);
 try {
 successCallback.apply(this, callbackArgs);
 } catch (ex) {
 console.error(ex);
 }
 }
 },
 failure: function (response) {
 console.error(response);
 },
 complete: function () {
 if (showLoader) {
 ACTIVE_REQ_CNT--;
 ACTIVE_REQ_CNT === 0 && fnHideWaitImage();
 }
 }
 });
}


//How to use GET method 

function fnGetData() {
 return fnCallAjaxHttpGetEvent('@Url.Action("ActionName", "ControllerName")', { paramId: paramId}, true, true, fnBindSucessFunction);
 }

function fnBindSuccessFunction(result){
  debugger;
}

Advertisements

Nested If else conditions in Kendo-UI Grid ClientTemplate

Standard Nested If else statement/ syntax used in Kendo-ui Grid

 

columns.Bound(c => c.ItemId)
.ClientTemplate(
“# if (condition-1) { #” +
“# if(condition-2) { #” +
“content-one”
“# } #” +
“#} else { #” +
“# if(condition-2) {#” +
“content-two”
“# } #” +
“# } #”
)

 

There are three ways to use the hash syntax:

  1. Render values as HTML: #= #.
  2. Use HTML encoding to display values: #: #.
  3. Execute arbitrary JavaScript code: # if (true) { # ... non-script content here ... # } #.

get id of closest ‘ul’ or parent id and get value of ‘href’ attribute with Jquery

html:

//if menu is created dynamically it is very useful
<ul id="slider" style="display: none;">
 <li class="panel cloned">test1</li>
 <li class="panel">
  <article>
   <h1><a href="/Admin/RoleMaster"> Role Creation </a> </h1>
  </article>
 </li>
 <li class="panel cloned">test2</li>
</ul>

js:

var url = window.location.pathname; //or window.location.toString();

$('#slider li.panel h1 a').each(function(){
   var myHref= $(this).attr('href');
   if( url == myHref) //compare current url with url value
  {

        var ulId = $(this).closest('ul').attr('id'); //get id of ul tag 
        $("#"+ulId).css("display", "block");
   }
});