Asp.net
Sample 1
// call ASP.NET
$(document).ready(function () {
$('#MyButton').click(function () {
$.post('../GetCustomers.aspx',
{PageSize:15},
function (data) {
$('#OutputDiv').html(data);
});
});
});
// GetCustomers.aspx
protected void Page_Load(object sender, EventArgs e)
{
var pageSize = (Request["PageSize"]==null)?50:Int32.Parse(Request["PageSize"]);
var custs = new List<Customer>();
for (int i = 1; i < pageSize; i++)
{
var cust = new Customer
{
FirstName = "John" + i.ToString(),
LastName = "Doe" + i.ToString()
};
custs.Add(cust);
}
dlCustomers.DataSource = custs;
dlCustomers.DataBind();
}
Sample 2
$.getJSON('../CustomerJson.aspx',{id:1},
function (data) {
alert('CustomerID: ' + data.ID + ' Name: ' +
data.FirstName + ' ' + data.LastName);
});
// CustomerJson.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
var cust = new Customer
{
ID = int.Parse(Request["id"]),
FirstName = "John",
LastName = "Doe"
};
var ser = new DataContractJsonSerializer(typeof(Customer));
ser.WriteObject(Response.OutputStream, cust);
}
ASP.NET-Related
ASP.NET MVC
Handling Exceptions using JQuery and ASp.NET MVC
Working with JQuery
private JsonResult ThrowJSONError(Exception e)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
//Log your exception
return Json(new { Message = e.Message});
}
public ActionResult DivideByZero()
{
try
{
throw new DivideByZeroException();
}
catch (DivideByZeroException e)
{
return ThrowJSONError(e);
}
}
$(document).ready(function() {
$.ajax({
type: "GET",
url: "AJAX/DivideByZero",
dataType: "json",
success: function(data) {
if (data) {
alert("Success!!!");
}
},
error: function(xhr, status, error) {
DisplayError(xhr);
}
});
});
function DisplayError(xhr) {
var msg = JSON.parse(xhr.responseText);
alert(msg.Message);
}
Alternative way:
$.ajax({
type: 'POST',
url: '/Account/UnfavoriteEvent',
data: { id: $("#EventID").val() },
success: function(data, textStatus, jqXHR) {
// jqXHR.status contains the Response.Status set on the server
},
error: function(jqXHR, textStatus, errorThrown) {
// jqXHR.status contains the Response.Status set on the server
}});
[HttpPost]
public void UnfavoriteEvent(int id)
{
try
{
var rows = _connection.Execute("DELETE UserEvent WHERE UserID = (SELECT up.UserID FROM UserProfile up WHERE up.UserName = @UserName) AND EventID = @EventID", new { EventID = id, UserName = User.Identity.Name });
if (rows != 1)
{
return new HttpStatusCodeResult(500, "There was an unknown error updating the database.");
}
}
catch (Exception ex) {
return new HttpStatusCodeResult(500, ex.Message);
}
}
Using with JQuery
Silk Project (Microsoft Pattern & Practices)
Run under MVC 3.0 (Required)
Learn how to use jQuery
- Codeproject JQuery Autocomplete MVC and ASP.NET
- CodeProject - Upload File Using Ajax and HTML 5 in MVC
- Reusable MVC Partial View with JQuery Databases
- CRUD Operations using Partial View and JQuery UI in ASP.NET MVC 4
- Improve perceived Performance of ASP.NET MVC Websites with Async Partial Views
- Filter and Display Data With ASP.NET MVC - Partial View and JQuery
- Master-detail Dropdown Lists and partial views with JQuery Ajax in MVC
- JQuery Ajax Request and MVC Detailed
- Model Dialog form View using JQuery ASP.NET MVC
- Two ways to render MVC 3 partial Views client side javascript or server side HTML helper
- ASP.NET MVC - AJAX-enabled Partial Views
- jqGrid Integration in MVC 4. using AJAX JSON JQuery
- Load dynamic grid within Partial View through Jquery Load
- Encapsulate Javascript completely in Partial View Jquery Widget\/MVC example
- extensions for asp net mvc
- MVC Basic Site - jQuery Integration in MVC 4.0 using AJAX, JSON, jQuery, LINQ
- Asp.NET MVC jQuery and Razor - Cascading dropdown retrieving patrial views, Json and objects, Handling Errors
- Master-detail Dropdown list and Partial Views with Jquery Ajax in MVC
- Ajax and jQuery in Asp.nET MVC
- Unobtrusive Ajax in ASP.NET MVC
- Retrieve JSON data from MVC Controllers in ASP.NET MVC
- Update an MVC Partial View with Ajax
- Filter and display data with ASP.NET MVC Partial View
- Code Project "How to Series" about MVC, JQuery, JSON , Paging, mapRoute
- Code Project JQuery DataTable and ASP.NET MVC Integration
- $.post and $.get in MVC Razor & JQuery
- Code Project - Full Calendar - A Complete Web Diary System for jQuery and C# MVC
- Cascading dropdown lists with MVC4 and Jquery
- Loading Partial View via Jquery