How to call Asp.Net MVC controller action method using JQuery

In this article i  am going to explain how to call controller actions using JQuery in Asp.Net MVC.

Get data from MVC Controller action method:


You can either use $.get or $.ajax functions to call mvc controller action methods using Jquery.

Example:

Assume you have following mvc controller and action method.

public class EmployeeController : Controller
    {
        public string GetEmployee()
        {
            var emp = new Employee() { Id = 1, Name = "John", Designation = "SE" };
            return Newtonsoft.Json.JsonConvert.SerializeObject(emp);
        }
    }

Above Employee Controller has one action method i.e., GetEmployee() which is returning serialized Employee data.

Now use the following JQuery code to call the above mvc action method

Method 1: Using $.get()

$.get("/Employee/GetEmployee", function (data) {
        var employee = JSON.parse(data);
        alert(employee.Name);
    });

Method 2: Using $.ajax()

$.ajax({
        url: "/Employee/GetEmployee",
        success: function (data) {
            var employee = JSON.parse(data);
            alert(employee.Name);
        }
    });

Note: I am using JSON.parse because controller action is returning serialized data. So we have to parse it to convert that to normal object.

Post data to MVC controller action methods using JQuery:


Using POST we can submit the data to controller actions.

Assume you have following controller action which saves the submitted data to database.

[HttpPost]
public bool SaveEmployee(Employee employee)
        {
            //your code to save employee to database
            //return true if data saved succesfully
         
            return true;
        }

Now use the following JQuery code to post data to controller action method.

Method 1: Using $.post()

//create the employee object
var employee = { Name: "Dave", Id: 2, Designation: "Sales" };

    $.post("Employee/SaveEmployee", employee, function (data) {
        if (data == "True") { //change condition based on your return type
            alert("Employee Saved succesfully");
        }
    });

Method 2: Using $.ajax()

//create the employee object
var employee = { Name: "Dave", Id: 2, Designation: "Sales" };

    $.ajax({
        url: "/Employee/SaveEmployee",
        type: "POST",
        data: employee,
        success: function (data) {
            if(data == "True") //change condition based on your return type
            alert("Employee Saved");
        }
    });

In this way we call Asp.Net MVC controller actions using JQuery.

For more posts on JQuery Visit: JQuery


Read more...

How to make Ajax call with JQuery

In this article am going to explain how to make ajax call with JQuery.

JQuery has $.ajax() method which is used to perform an asynchronous HTTP request.

The syntax of $.ajax() function is as follows

$.ajax(url[, options])

url is a string parameter that you want to reach with AJAX call while options is an object literal containing the configuration for the Ajax request.

JQuery AJAX example:

$.ajax({
  url: "demo.txt",
  success: function(result){
    alert(result);
  }
});

JQuery AJAX post example:

$.ajax({
  type: "POST",
  url: url,
  data: {name:"ranadheer",id:2},
  success: function(data, status,, jQxhr){
     alert(data);
  },
  error: function(jQxhr, textStatus){
     alert(error);
  }
  dataType: dataType
});


This way we can make AJAX calls with JQuery.

For more posts on JQuery visit: JQuery

Read more...

How to read query string value in javascript

In this article am going to explain how to read query string value using JavaScript.

Use the following function to read query string value from url. You just need to pass the parameter name to this function.

Method 1:

var getQueryString = function ( field) {
    var href = window.location.href;
    var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
    var string = reg.exec(href);
    return string ? string[1] : null;
};

Method 2 :

You can also use the following javascript function to read querystring value.
function getQueryString(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}

For example assume url as http://mysite.com?myParam1=test1&myParam2=test2

Now to read the query string value from above url use the following javascript code.

var myParam1 = getQueryString('myParam1'); // returns 'test1'
var myParam2 = getQueryString('myParam2'); // returns 'test2'
var testParam = getQueryString('something'); // returns null

Note: If a parameter is present several times (?param=test1&param=test2), you will get the first value


In this way we can read query string value from url in JavaScript


For more posts on javascript visit: JavaScript


Read more...

Using CDN Bundle Config in ASP.NET MVC

This article explains how to use CDN in MVC script bundles.

Bundling is a technique used in ASP.NET 4.5 to improve request load time. This technique improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript).

If you are using libraries like JQuery, AngularJS in your application, instead of loading them from your own server you can use the CDN url of these libraries. So, when you run your application in production environment these files will be loaded from CDN itself. If the CDN is not available then these files will be loaded from your server.

How to add CDN in MVC bundles:


First of all enable cdn feature (by default it is set to false).

bundles.UseCdn = true;

 Then add your library (JQuery or AngularJS or any other) bundle from CDN. In my example am using JQuery library

var jqueryBundle = new ScriptBundle("~/bundles/jquery", "http://code.jquery.com/jquery-2.0.3.min.js").Include(
                "~/Scripts/jquery-{version}.js");
            jqueryBundle.CdnFallbackExpression = "window.jquery";
            bundles.Add(jqueryBundle);

Use of CdnFallbackExpression is when CDN server is unavailable it loads the file from your server.

In this way we can use CDN in MVC script bundles.

Test CDN bundle in Local Evnironment (Debug Mode):


To test this feature locally you have to run your application in debug="false" mode or you cal also use BundleTable.EnableOptimizations = true;

For more posts on MVC visit : Asp.Net MVC

Read more...

How to find out which version of Microsoft Dynamics AX you are using

To find what version of the Microsoft Dynamics AX you are using follow the below steps

  1. Click on Help Icon in the upper right corner of any page in Microsoft Dynamics AX


  2. 2. Now click on About Microsoft Dynamics AX


  3. You can check the version in the resulting window.
For more information about the Microsoft Dynamics AX build version check below post
Overview of Microsoft Dynamics AX build numbers

For quick over view of build numbers and versions check below

Dynamics AX 2012

Build Number>>Version

6.0.852.782012>> Beta

6.0.947.02012>> RTM

6.0.947.612012>> Cumulative Update 1 (CU1)

6.0.947.2802012>> Cumulative Update 2 (CU2)

6.0.947.8622012>> Feature Pack

6.0.1108.6702012>> Cumulative Update 3 (CU3)

6.0.1108.24232012>> Cumulative Update 4 (CU4)

6.0.1108.43162012>> Cumulative Update 5 (CU5)

6.2.158.02012>> R2

6.2.1000.1562012>> R2 Cumulative Update 1 (CU1)

6.2.1000.14372012>> R2 Cumulative Update 6 (CU6)

6.2.1000.40512012>> R2 Cumulative Update 7 (CU7)

6.2.1000.40512012>> R2 Cumulative Update 7 (CU7)

6.3 >> Dynamics AX 2012 R3

Read more...