When writing MVC code you cannot forget jQuery. jQuery provides a substantial contribution to make your application faster.
The following is what is to be discussed:
- using $.get()
- using $.post()
- using $.ajax()
- using URL (JavaScript style)
When you are writing jQuery code, one thing to keep in your mind is that you need to minimize page loading/refreshing.
$.get() method
- Calling Controller Action without parameter.
Controller:
View:- public string SaveEmployeeRecord()
- {
- string res = "this is return value";
- // do here some operation
- return res;
- }
- $.get("/Home/SaveEmployeeRecord",null, function (data) {
- alert(data);
- });
- Calling Controller Action with parameter.
Controller:
View:- public string SaveEmployeeRecord(string name)
- {
- string res = name;
- // do here some operation
- return res;
- }
- $.get("/Home/SaveEmployeeRecord", {name:'Deepak'}, function (data) {
- alert(data);
- });
- Calling Controller Action with parameter returning JSON data.
Controller:
View:- public JsonResult SaveEmployeeRecord(string id,string name)
- {
- string this_id = id;
- string this_name = name;
- // do here some operation
- return Json(new {id=this_id,name = this_name },JsonRequestBehavior.AllowGet);
- }
- $.get("/Home/SaveEmployeeRecord", {id:'67',name:'Deepak'}, function (data) {
- alert(data.id); // display id value which is returned from the action method
- alert(data.name);//display name value which is returned from the action method
- });
$.post() method
The syntax and use of the post method is just like the get method. Here instead of using the get keyword, use the post keyword and all the other things are the same.
- Calling Controller Action without parameter.
Controller:
View:- public string SaveEmployeeRecord()
- {
- string res = "this is return value";
- // do here some operation
- return res;
- }
- $.post("/Home/SaveEmployeeRecord",null, function (data) {
- alert(data);
- });
- Calling Controller Action with parameter.
Controller:
View:- public string SaveEmployeeRecord(string name)
- {
- string res = name;
- // do here some operation
- return res;
- }
- $.post("/Home/SaveEmployeeRecord", {name:'Deepak'}, function (data) {
- alert(data);
- });
- Calling Controller Action with parameter returning JSON data.
Controller:
View:- public JsonResult SaveEmployeeRecord(string id,string name)
- {
- string this_id = id;
- string this_name = name;
- // do here some operation
- return Json(new {id=this_id,name = this_name },JsonRequestBehavior.AllowGet);
- }
- $.post("/Home/SaveEmployeeRecord", {id:'67',name:'Deepak'}, function (data) {
- alert(data.id); // display id value which is returned from the action method
- alert(data.name);//display name value which is returned from the action method
- });
$.ajax() method
- Calling Controller Action with parameter returning JSON data.
Controller:
View:- public JsonResult SaveEmployeeRecord(string id,string name)
- {
- string this_id = id;
- string this_name = name;
- // do here some operation
- return Json(new {id=this_id,name = this_name },JsonRequestBehavior.AllowGet);
- }
- $.ajax({
- type: 'POST',
- dataType: 'json',
- url: '/Home/SaveEmployeeRecord',
- data: { id: '67', name: 'Deepak' },
- success: function (Data) {
- alert(data.id);
- alert(data.name);
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- }
- });


Deepak Kumar JenaPosted Feb 28, 2020, 7:30 AM EST
$file ="D:\\Hello-demo.pdf" $desiredSize = 99 #for 100 kb/ minus 1 from desired kb $mockData="dump data. ignore this" $currentSize = ($mockData).length/1KB while($currentSize -lt $desiredSize) { $mockData+=$mockData $currentSize = ($mockData).length/1KB } $mockData =[string]$mockData while($currentSize -ge $desiredSize) { $mockData= $mockData.Substring(0,$mockData.Length-1) $currentSize = ($mockData).length/1KB } Set-Content $file $mockData -force
Deepak Kumar JenaPosted Feb 28, 2020, 7:15 AM EST
$file ="D:\\Hello-demo.pdf" $desiredSize = 99 #for 100 kb/ minus 1 from desired kb $mockData="dump data. ignore this" $currentSize = ($mockData).length/1KB while($currentSize -lt $desiredSize) { $mockData+=$mockData $currentSize = ($mockData).length/1KB } $mockData =[string]$mockData while($currentSize -ge $desiredSize) { $mockData= $mockData.Substring(0,$mockData.Length-1) $currentSize = ($mockData).length/1KB } Set-Content $file $mockData -force
Deepak Kumar JenaPosted Jul 17, 2019, 2:38 AM EDT
.....................
Catherine OborskiPosted Jul 13, 2018, 4:21 AM EDT
Do you have have a callback function? Can you not pass one, or pass null in its place?
NehaPosted Dec 29, 2016, 4:05 AM EST
Nice one..............................
NehaPosted Dec 29, 2016, 4:05 AM EST
Nice................................................
RakeshPosted Oct 18, 2016, 9:58 AM EDT
Good one
kalu singh raoPosted Jul 19, 2016, 5:35 AM EDT
Nice...
Deepak Kumar JenaPosted Jul 18, 2015, 6:25 AM EDT
#FF0000
Sagar pandilwarPosted Feb 10, 2015, 3:55 AM EST
Good job..
Deepak VermaPosted Dec 15, 2014, 10:11 PM EST
Very good start Deepak.... nice article.
shivanagPosted Dec 15, 2014, 10:10 PM EST
Thanks Deepak it is very useful...
Phani BharadwajPosted Dec 15, 2014, 10:08 PM EST
It is useful for MVC developers...