<?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; properties</title>
	<atom:link href="http://www.marcdormey.com/index.php/tags/properties/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>
	</channel>
</rss>
