<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marc Dormey &#187; ajax</title>
	<atom:link href="http://www.marcdormey.com/index.php/tags/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://www.marcdormey.com</link>
	<description>Software Finds, Programming Tutorials and Gadget Reviews</description>
	<lastBuildDate>Tue, 20 Jul 2010 14:52:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Update Form Fields with JSON result in ASP.NET MVC</title>
		<link>http://www.marcdormey.com/index.php/archives/update-form-fields-with-json-result-in-asp-net-mvc</link>
		<comments>http://www.marcdormey.com/index.php/archives/update-form-fields-with-json-result-in-asp-net-mvc#comments</comments>
		<pubDate>Mon, 05 Oct 2009 10:28:21 +0000</pubDate>
		<dc:creator>Marcel</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[properties]]></category>

		<guid isPermaLink="false">http://www.marcdormey.com/?p=217</guid>
		<description><![CDATA[Wouldn&#8217;t it be nifty to receive a JSON result object from AJAX and it automagically updates your form fields? Those who said &#8220;No&#8221; leave now!
I had to find a way to iterate through my JSON properties and, if I named the fields the same as the JSON properties, update them all in one go.
Lets use [...]]]></description>
			<content:encoded><![CDATA[<p>Wouldn&#8217;t it be nifty to receive a JSON result object from AJAX and it automagically updates your form fields? Those who said &#8220;No&#8221; leave now!<br />
I had to find a way to iterate through my JSON properties and, if I named the fields the same as the JSON properties, update them all in one go.</p>
<p>Lets use a simple &#8220;Person&#8221; model and have a look at the ASP.NET MVC action:</p>
<pre class="brush:csharp">
public ActionResult GetPerson(int id)
{
    var person = Context.GetPerson(id);
    return Json(new
                    {
                        success = true,
                        model = new
                                    {
                                        person.Id,
                                        person.Name,
                                        person.Surname,
                                        person.Age,

                                    }
                    });
}
</pre>
<p>As you can see this returns a DTO (data transfer object) as JSON result and is ready to be consumed via AJAX&#8230;</p>
<pre class="brush:js">
$.getJSON("/Person/1", function(data) {
    if (data.success) {
        $.updateForm(data.model);
    } else {
        alert("An error occurred");
    }
});
</pre>
<p>All we then need is the magical &#8220;$.updateForm&#8221; method that contains all the voodoo:</p>
<pre class="brush:js">
jQuery.updateForm = function(model) {
   // we iterate through all the properties
   for (var property in model) {
        // evaluate the expression to get the value
        var propertyValue = eval("model." + property);
        // and update the field
        $("#" + property).val(propertyValue);
    }
};
</pre>
<p>Currenty this will work fine for textboxes and textareas as this was all the functionality I needed for now. If anyone extends this for &#8220;select&#8221;, &#8220;checkbox&#8221;, &#8220;radiobutton&#8221; etc&#8230; please share with us so I can update this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marcdormey.com/index.php/archives/update-form-fields-with-json-result-in-asp-net-mvc/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JQuery function to populate a select box</title>
		<link>http://www.marcdormey.com/index.php/archives/jquery-function-to-populate-a-select-box</link>
		<comments>http://www.marcdormey.com/index.php/archives/jquery-function-to-populate-a-select-box#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:52:48 +0000</pubDate>
		<dc:creator>Marcel</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.marcdormey.com/?p=34</guid>
		<description><![CDATA[Something you might be repeating while building a user interface is populating a select box with JSON results. Here is a quick demonstration on a very simple way to do this. Put the &#8216;populateSelect&#8217; method in a global .js file and use it throughout your project as &#8216;$.populateSelect(args&#8230;)&#8217;
ASP.NET MVC Action Method
public ActionResult GetUsers()
{
   [...]]]></description>
			<content:encoded><![CDATA[<p>Something you might be repeating while building a user interface is populating a select box with JSON results. Here is a quick demonstration on a very simple way to do this. Put the &#8216;populateSelect&#8217; method in a global .js file and use it throughout your project as &#8216;$.populateSelect(args&#8230;)&#8217;</p>
<p>ASP.NET MVC Action Method</p>
<pre class="brush:csharp">public ActionResult GetUsers()
{
     var data = Service.QueryUsers()
           .Select(x =&gt; new {value = x.Id, text = x.Name});

      return Json(data);
}</pre>
<p>Usage:</p>
<pre class="brush:js">var url = "&lt;%=Url.Action("GetUsers", "UserController") %&gt;";

$.getJSON(url, function(data, textstatus) {
        $.populateSelect("#someSelectBox", data);
});
</pre>
<p>Global Method:</p>
<pre class="brush:js">
// JQuery Method
jQuery.populateSelect = function(selectId, data) {
    $.each(data, function() {
        var option = new Option(this.text, this.value);
        var dropdownList = $(selectId)[0];
        if ($.browser.msie) {
            dropdownList.add(option);
        } else {
            dropdownList.add(option, null);
        }
    });
};</pre>
<p>Removing all Items from a select list is just as simple:</p>
<pre class="brush:js">
$("#mySelectList")
   .find("option")
   .remove();
</pre>
<p>And lastly how to Append or Prepend items to the list:</p>
<pre class="brush:csharp">
// Add options to the end of a select
$("#mySelectList")
    .append("<option value='1'>New Item</option>");

// Add options to the start of a select
$("#mySelectList")
    .prepend("<option value='0'>Select...</option>");
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.marcdormey.com/index.php/archives/jquery-function-to-populate-a-select-box/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turn your hyperlinks into ajax calls</title>
		<link>http://www.marcdormey.com/index.php/archives/two-jquery-methods-i-cannot-live-without</link>
		<comments>http://www.marcdormey.com/index.php/archives/two-jquery-methods-i-cannot-live-without#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:39:32 +0000</pubDate>
		<dc:creator>Marcel</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[href]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.marcdormey.com/?p=23</guid>
		<description><![CDATA[When designing a rich user interface with JQuery it is very useful to replace default hyperlink functionality with ajax calls. The first method will turn the &#60;a&#62; functionality into an ajax call using the &#8220;href&#8221; attribute as source. The second method will do the same, but will ask &#8220;Are you sure?&#8221; first (very usefull for [...]]]></description>
			<content:encoded><![CDATA[<p>When designing a rich user interface with JQuery it is very useful to replace default hyperlink functionality with ajax calls. The first method will turn the &lt;a&gt; functionality into an ajax call using the &#8220;href&#8221; attribute as source. The second method will do the same, but will ask &#8220;Are you sure?&#8221; first (very usefull for deletes etc&#8230;).</p>
<p>The ajaxReload var can then be set to a function that would handle the ajaxCall. These methods would usually be located in a &#8216;common.js&#8217; file.</p>
<pre class="brush:js">
// GLOBAL VARS
var ajaxReload = function() { };

$(".ajaxCall").live("click", function(e) {
    e.preventDefault();
    $.get($(this).attr("href"), ajaxReload);
});

$(".yesNoAjaxCall").live("click", function(e) {
    e.preventDefault();
    if (confirm('Are you sure?')) {
        $.get($(this).attr("href"), ajaxReload);
    }
});
</pre>
<p>The reason behind using &#8216;live&#8217; instead of &#8216;bind&#8217; or &#8216;click&#8217; is because I want to bind all future loaded &#8216;a&#8217; tags too. Usually &#8216;bind&#8217; or &#8216;click&#8217; will only find the static &#8216;a&#8217; tags that are available on page load.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marcdormey.com/index.php/archives/two-jquery-methods-i-cannot-live-without/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
