a developer's notes – a semi-technical web development BLOG

May 23, 2013

Sample jquery ajax Calls

Filed under: Javascript / JQuery — Duy Nguyen @ 3:47 pm
Tags: , , ,

Sample jQuery ajax POST:

$('#buttonId').click(function () {
	$.ajax({
		type: 'POST',
		url: ApplicationRoot + 'controller/PostMethod',
		data: JSON.stringify({ Name: $('#Name').val(), Address: $('#Address').val() }),
		contentType: 'application/json',
		dataType: 'json',
		success: aCallBackFunctionName
	});
});

[HttpPost]
public JsonResult PostMethod(string Name, string Address)
{
	if (!String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(Address))
	{
        //TODO: Save the data in database
        return Json("ok");
	}
    else
	{
        return "Please complete the form.";
	}
}

Sample jQuery ajax GET:

$.ajax({
	url: ApplicationRoot + 'controller/GetMethod',
	type: 'GET',
	data: { selectedId : id},
	dataType: 'json',
	error: function (xhr, textStatus, errorThrown) {
		//alert(xhr.status);
		//alert(thrownError);
	},
	success: function (data) {
		//do something
	}
});

[HttpGet]
public JsonResult GetMethod()
{
	// do something
	var data = 1;
	return Json(data, JsonRequestBehavior.AllowGet);
}

If you’re using the Anti-Forgery Requests for HttpPost then do this..

		var tokenVal = $('input[name=__RequestVerificationToken]').val();
		
        $('#buttonId').click(function () {
            $.ajax({
                type: 'POST',
                url: ApplicationRoot + 'controller/PostMethod',
                data: JSON.stringify({ filter: $('#Filter').val(), itemIDs: $('#items').val(), __RequestVerificationToken : tokenVal}),
                contentType: 'application/json',
                dataType: 'json',
                success: aCallBackFunctionName
            });
        });

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult PostMethod(string Name, string Address)
{
	if (!String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(Address))
	{
        //TODO: Save the data in database
        return Json("ok");
	}
    else
	{
        return "Please complete the form.";
	}
}

April 11, 2013

How to get a radio button’s label text with jQuery

Filed under: Javascript / JQuery — Duy Nguyen @ 9:59 pm
Tags: , , , , , ,

From stackoverflow

<input type="radio" name="value" id="product1">
<label for="product1">Product One</label>

You can obtain the label’s text with this jQuery code:

var text = $("input:checked + label").text();

The + signifies the adjacent selector. It will only select a label which has a checked input next to it.

If your label element has to be placed before the input, a single CSS selector can’t get a reference to it. Using jquery, you can however get a reference to the “previous child element” with the prev() function.

var text = $("input:checked").prev().text();

September 5, 2011

Two ways to get an Enum with a string

Filed under: C# — Duy Nguyen @ 2:33 pm
Tags: , , ,

Two ways to get an Enum. Consider the following Enum:

 public enum MyEnumType
    {
        Default = 0,
        First,
        Second,
        Third,
    }

How to get an Enum type from a string equivalent to the Enum value?

(MyEnumType)Enum.ToObject(typeof(MyEnumType), Convert.ToInt16('1'));

// Gives you MyEnumType.First

How to get an Enum type from a string equivalent to the Enum name?

(MyEnumType)Enum.Parse(typeof(MyEnumType), 'Second', true); 

//passing in true as the third parameter makes it not case-sensitive
// Gives you MyEnumType.Second

How to get a value from an Enum?

int enumVal = (int)MyEnumType;

Get a Javascript DOM object from a jQuery object

Filed under: Javascript / JQuery — Duy Nguyen @ 1:49 pm
Tags: , , , , ,

jQuery object is a wrapper around the js DOM object. If you want to get the DOM object from the jQuery object, you should use get() method.

For example, if the jQuery object includes only one DOM object, you can use

$("div").get(0)

or

$("div")[0]

However, if you have multiple DOM objects, you can use

$("div").get()

var myvar;
myvar= $(‘#<%=selectedProductionIdsHdnVl.ClientID%>‘).get(0);
myvar.value = “b”;

same as

document.getElementById(“<%=selectedProductionIdsHdnVl.ClientID%>“).value = “B”;

This allowed me to use the ASP Hidden field and have the OnValueChanged server side event get fired.

Blog at WordPress.com.