Return to Snippet

Revision: 36603
at November 24, 2010 03:37 by housecor


Updated Code
/// <summary>
/// Return list of anonymous product highlight objects. 
/// </summary>
/// <param name="productID"></param>
/// <returns></returns>
[WebMethod]
public static List<object> GetHighlights(int productID)
{
	FTJEntities context = new FTJEntities();
	return (from h in context.ProductHighlights where h.ProductID == productID select new { h.Description, h.DisplaySequence }).ToList<object>();
}

//jquery to send request:
$.ajax({
	type: "POST",
	url: "/Admin/Products/Overview.aspx/GetHighlightsf",
	data: "{productID: '" + $('#hfProductID').val() + "' }",
	contentType: "application/json; charset=utf-8",
	dataType: "json",
	success: function (msg) {
		if (msg.d == "false") alert('There was an error retrieving product highlights via ajax.');
		var highlights = msg.d;
		$.each(highlights, function (index, highlight) {
			RenderHighlight(highlight);
		});
	},
	failure: function (msg) {
		alert('Error: ' + msg);
	}
});

//the function that renders the new table row:
function RenderHighlight(highlight) {
	highlightRow = '<tr><td><input type="text" name="highlight" value="' + highlight.Description + '" class="text-input large-input"/></td><td class="tools"><a href="#" class="add">Add</a><a href="#" class="remove" rel="">Remove</a></td></tr>';
	$('#highlights tbody:last').append(highlightRow);

	if ($('a.add').size() == 1) {
		$('a.remove').hide();
	}
}

Revision: 36602
at November 24, 2010 03:36 by housecor


Updated Code
/// <summary>
/// Return list of anonymous product highlights objects. 
/// </summary>
/// <param name="productID"></param>
/// <returns></returns>
[WebMethod]
public static List<object> GetHighlights(int productID)
{
	FTJEntities context = new FTJEntities();
	return (from h in context.ProductHighlights where h.ProductID == productID select new { h.Description, h.DisplaySequence }).ToList<object>();
}

//jquery to send request:
$.ajax({
	type: "POST",
	url: "/Admin/Products/Overview.aspx/GetHighlightsf",
	data: "{productID: '" + $('#hfProductID').val() + "' }",
	contentType: "application/json; charset=utf-8",
	dataType: "json",
	success: function (msg) {
		if (msg.d == "false") alert('There was an error retrieving product highlights via ajax.');
		var highlights = msg.d;
		$.each(highlights, function (index, highlight) {
			RenderHighlight(highlight);
		});
	},
	failure: function (msg) {
		alert('Error: ' + msg);
	}
});

//the function that already renders the new table row:
function RenderHighlight(highlight) {
	highlightRow = '<tr><td><input type="text" name="highlight" value="' + highlight.Description + '" class="text-input large-input"/></td><td class="tools"><a href="#" class="add">Add</a><a href="#" class="remove" rel="">Remove</a></td></tr>';
	$('#highlights tbody:last').append(highlightRow);

	if ($('a.add').size() == 1) {
		$('a.remove').hide();
	}
}

Revision: 36601
at November 24, 2010 03:35 by housecor


Updated Code
/// <summary>
/// Return list of anonymous product highlights objects. 
/// </summary>
/// <param name="providerID"></param>
/// <returns></returns>
[WebMethod]
public static List<object> GetHighlights(int productID)
{
	FTJEntities context = new FTJEntities();
	return (from h in context.ProductHighlights where h.ProductID == productID select new { h.Description, h.DisplaySequence }).ToList<object>();
}

//jquery to send request:
$.ajax({
	type: "POST",
	url: "/Admin/Products/Overview.aspx/GetHighlightsf",
	data: "{productID: '" + $('#hfProductID').val() + "' }",
	contentType: "application/json; charset=utf-8",
	dataType: "json",
	success: function (msg) {
		if (msg.d == "false") alert('There was an error retrieving product highlights via ajax.');
		var highlights = msg.d;
		$.each(highlights, function (index, highlight) {
			RenderHighlight(highlight);
		});
	},
	failure: function (msg) {
		alert('Error: ' + msg);
	}
});

//the function that already renders the new table row:
function RenderHighlight(highlight) {
	highlightRow = '<tr><td><input type="text" name="highlight" value="' + highlight.Description + '" class="text-input large-input"/></td><td class="tools"><a href="#" class="add">Add</a><a href="#" class="remove" rel="">Remove</a></td></tr>';
	$('#highlights tbody:last').append(highlightRow);

	if ($('a.add').size() == 1) {
		$('a.remove').hide();
	}
}

Revision: 36600
at November 24, 2010 03:34 by housecor


Initial Code
/// <summary>
/// Return list of anonymous product highlights objects. 
/// </summary>
/// <param name="providerID"></param>
/// <returns></returns>
[WebMethod]
public static List<object> GetHighlights(int productID)
{
	FTJEntities context = new FTJEntities();
	return (from h in context.ProductHighlights where h.ProductID == productID select new { h.Description, h.DisplaySequence }).ToList<object>();
}

//jquery to send request:
$.ajax({
	type: "POST",
	url: "/Admin/Products/Overview.aspx/GetHighlightsf",
	data: "{productID: '" + $('#hfProductID').val() + "' }",
	contentType: "application/json; charset=utf-8",
	dataType: "json",
	success: function (msg) {
		if (msg.d == "false") alert('There was an error retrieving product highlights via ajax.');
		var highlights = msg.d;
		alert(highlights);
		$.each(highlights, function (index, highlight) {
			RenderHighlight(highlight);
		});
	},
	failure: function (msg) {
		alert('Error: ' + msg);
	}
});

Initial URL
http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery

Initial Description


Initial Title
JQuery to parse array of objects sent from Webmethod

Initial Tags


Initial Language
C#