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.";
	}
}
Advertisements

May 7, 2013

Javascript function to convert number to currency.

Filed under: Javascript / JQuery — Duy Nguyen @ 4:25 pm
Tags: , , ,

Read Here


function format1(n, currency) {
    return currency + " " + n.toFixed(2).replace(/./g, function(c, i, a) {
        return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
    });
}

function format2(n, currency) {
    return currency + " " + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
}

var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67];

document.write("<p>Format #1:</p>");
for (var i = 0; i < numbers.length; i++) {
    document.write(format1(numbers[i], "£") + "<br />");
}

document.write("<p>Format #2:</p>");
for (var i = 0; i < numbers.length; i++) {
    document.write(format2(numbers[i], "$") + "<br />");
}

Here is the output

Format #1:
£ 1.00
£ 12.00
£ 123.00
£ 1,234.00
£ 12,345.00
£ 123,456.00
£ 1,234,567.00
£ 12,345.67

Format #2:
$ 1.00
$ 12.00
$ 123.00
$ 1,234.00
$ 12,345.00
$ 123,456.00
$ 1,234,567.00
$ 12,345.67

Pass Hidden Input from View to MVC Controller

Filed under: ASP.NET,C# — Duy Nguyen @ 11:51 am
Tags: , , , , ,

Make sure your input has an id AND name attribute. Having just the id attribute will not work.

<% using (Html.BeginForm("MyAction", "Controller", FormMethod.Post, new { id = "formId", target = "_self" })) 
   { %>
     <input type="hidden" name="myHidden" id="myHidden" value="false" />
<% } %>

Then in the controller, access the form values like this.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyAction(FormCollection form)
{
     string myHiddenInput= form["myHidden"];
}

Create a free website or blog at WordPress.com.