<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Stefan's Tech Notes</title>
	
	<link>http://www.stefanprodan.eu</link>
	<description>about life on internet, new technologies, software &amp; .net programming</description>
	<lastBuildDate>Mon, 13 Feb 2012 20:06:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/StefanTechNotes" /><feedburner:info uri="stefantechnotes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Parallel processing of concurrent Ajax requests in Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/bVp2jW0ST6Y/</link>
		<comments>http://www.stefanprodan.eu/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 16:34:09 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=641</guid>
		<description><![CDATA[If you develop web applications that relies on JavaScript to update the interface and you use several Ajax calls inside a single page, then you should know that all those...]]></description>
			<content:encoded><![CDATA[<p>If you develop web applications that relies on JavaScript to update the interface and you use several Ajax calls inside a single page, then you should know that all those calls are not executed in parallel on the server. On a default controller, each call to an action locks the user’s Session object for synchronization purposes, so even if you trigger three calls at once from the browser, these calls will get queued and executed one by one.</p>
<p>In order to speed up the requests processing you should reduce the number of actions that needs to write in the Session object or TempData to a minimum and make a dedicated controller for them. The rest of the actions can now be executed in parallel using controllers that are only reading values from Session.&#160; </p>
<p><img class="alignnone size-full wp-image-643" title="parallel-ajax-time" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2012/02/parallel-ajax-time.png" width="584" height="276" /></p>
<p>If you want to enable a certain controller to process requests in parallel from a single user, you can use an attribute named <em><strong>SessionState</strong></em> that was introduced in MVC since version 3. It’s not necessary to use a session-less controller in order to achieve parallelism, the Ready-only option of the <em><strong><a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatebehavior.aspx" target="_blank">SessionStateBehavior</a></strong></em> will let you pass security checks you may have implemented based on Session data. </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:caaa7535-7377-4afb-8806-ba8577dd339a" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">ParallelController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    [<span style="color:#2b91af">SessionState</span>(System.Web.SessionState.<span style="color:#2b91af">SessionStateBehavior</span>.ReadOnly)]<br />     [<span style="color:#2b91af">OutputCache</span>(NoStore = <span style="color:#0000ff">true</span>, Duration = 0, VaryByParam = <span style="color:#a31515">&quot;*&quot;</span>)]<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">ParallelController</span> : <span style="color:#2b91af">Controller</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get1()<br />         {<br />             <span style="color:#008000">//read from session</span><br />             <span style="color:#0000ff">var</span> x = Session[<span style="color:#a31515">&quot;call&quot;</span>];<br />             <span style="color:#2b91af">Thread</span>.Sleep(5000);<br />             <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span> { obj = <span style="color:#a31515">&quot;p1&quot;</span> }, <span style="color:#2b91af">JsonRequestBehavior</span>.AllowGet);<br />         }</p>
<p>         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get2()&#8230;<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get3()&#8230;<br />     }</div>
</p></div>
</p></div>
<p>The serial controller has the same actions but writes data in the session object:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:323b95b0-545b-46a9-86c7-a6dde52cca7b" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">SerialController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    [<span style="color:#2b91af">SessionState</span>(System.Web.SessionState.<span style="color:#2b91af">SessionStateBehavior</span>.Required)]<br />     [<span style="color:#2b91af">OutputCache</span>(NoStore = <span style="color:#0000ff">true</span>, Duration = 0, VaryByParam = <span style="color:#a31515">&quot;*&quot;</span>)]<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">SerialController</span> : <span style="color:#2b91af">Controller</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get1()<br />         {<br />             <span style="color:#008000">//write to session</span><br />             Session[<span style="color:#a31515">&quot;call&quot;</span>] = <span style="color:#a31515">&quot;call&quot;</span>;<br />             <span style="color:#2b91af">Thread</span>.Sleep(5000);<br />             <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span> { obj = <span style="color:#a31515">&quot;s1&quot;</span> }, <span style="color:#2b91af">JsonRequestBehavior</span>.AllowGet);<br />         }</p>
<p>         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get2()&#8230;<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> Get3()&#8230;<br />     }</div>
</p></div>
</p></div>
<p>Calling both controllers from jQuery at document ready:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:14ed6b64-9c77-4309-adaf-533b5ed7e1ae" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    $(document).ready(<span style="color:#0000ff">function</span> () {</p>
<p>         start = <span style="color:#0000ff">new</span> Date().getTime();</p>
<p>         <span style="color:#0000ff">for</span> (i = 1; i &lt;= 3; i++) {<br />             $.ajax(<span style="color:#800000">&#39;Parallel/Get&#39;</span> + i, {<br />                 cache: <span style="color:#0000ff">false</span><br />             }).done(logParallel);<br />         }</p>
<p>         <span style="color:#0000ff">for</span> (i = 1; i &lt;= 3; i++) {<br />             $.ajax(<span style="color:#800000">&#39;Serial/Get&#39;</span> + i, {<br />                 cache: <span style="color:#0000ff">false</span><br />             }).done(logSerial);<br />         }</p>
<p>     });</p></div>
</p></div>
</p></div>
<p>In Firebug you can see on the Network tab, that all six calls are fired at once and all calls made to the parallel (SessionStateBehavior.ReadOnly) controller returns in 5 seconds along with the first call that reached the serial (Read-Write Session) controller. The Get2 call to the serial controller will wait for Get1 to complete, and Get3 will wait for Get2, lasting 10 seconds longer overall. The Firebug timeline is more accurate then my js date diff. </p>
<p><img class="alignnone size-full wp-image-642" title="parallel-ajax-fb" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2012/02/parallel-ajax-fb.png" width="533" height="211" /></p>
<p>The result showed here where taken with the project hosted on a dedicated Webserver. On my home pc, sometimes, the results got messed up because the browser can fire some calls&#160; too late or, on the server side, the call is queued inside the <em>ThreadPull</em> for a thread that is already busy. Azure would be a good place to host this test.</p>
<p>Prerequisites &amp; Download</p>
<ul>
<li><a href="http://www.microsoft.com/download/en/details.aspx?id=23691">VS.NET 2010 SP1</a> </li>
<li><a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=27419">ASP.NET MVC 4 Developer Preview</a> </li>
</ul>
<p>In order to easily install all the prerequisites you can use the <a href="http://www.microsoft.com/web/">Web Platform Installer</a> version 4.</p>
<p><a href="http://www.stefanprodan.eu/admin/code/ConcurrentAjax.zip"><img title="download source code" alt="download source code files" src="http://www.stefanprodan.eu/wp-content/uploads/2011/02/downloadsc.png" width="186" height="22" /></a></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/bVp2jW0ST6Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>User friendly CAPTCHA for Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/hLBqjZpDdec/</link>
		<comments>http://www.stefanprodan.eu/2012/01/user-friendly-captcha-for-asp-net-mvc/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 23:27:38 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=626</guid>
		<description><![CDATA[In this post I will show you how to add CAPTCHA functionality to a html form in an Asp.Net MVC 4 project. My goal is to make the CAPTCHA problem...]]></description>
			<content:encoded><![CDATA[<p>In this post I will show you how to add CAPTCHA functionality to a html form in an Asp.Net MVC 4 project. My goal is to make the CAPTCHA problem easy enough for all to solve, like a simple sum operation, and easier to read then the standard CAPTCHA text. An easy to read image is more vulnerable to smart bots that have an ORC system but I prefer to scare less clients then to provide the strongest anti-bot protection. And one more feature, when clicked the image should, change giving the users a new chance to respond correctly. </p>
<p><img class="alignnone size-full wp-image-627" title="CaptchaMvc" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2012/01/CaptchaMvc.png" width="352" height="305" /></p>
<p>Implementing CAPTCHA in C# and MVC 4 takes these steps:</p>
<ul>
<li>Create an Action that returns a CAPTCHA image and stores in the user session the right answer </li>
<li>Add to your Model a string property named Captcha </li>
<li>Add to your View the textbox for Captcha and the image placeholder </li>
<li>Validate answer inside your own Action </li>
</ul>
<p>&#160;</p>
<h5>Render CAPTCHA image</h5>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74d9cebc-b718-43d8-b90c-bc52e2b5e850" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">CaptchaController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">        <span style="color:#0000ff">public</span> <span style="color:#2b91af">ActionResult</span> CaptchaImage(<span style="color:#0000ff">string</span> prefix, <span style="color:#0000ff">bool</span> noisy = <span style="color:#0000ff">true</span>)<br />         {<br />             <span style="color:#0000ff">var</span> rand = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Random</span>((<span style="color:#0000ff">int</span>)<span style="color:#2b91af">DateTime</span>.Now.Ticks);</p>
<p>             <span style="color:#008000">//generate new question</span><br />             <span style="color:#0000ff">int</span> a = rand.Next(10, 99);<br />             <span style="color:#0000ff">int</span> b = rand.Next(0, 9);<br />             <span style="color:#0000ff">var</span> captcha = <span style="color:#0000ff">string</span>.Format(<span style="color:#a31515">&quot;{0} + {1} = ?&quot;</span>, a, b);</p>
<p>             <span style="color:#008000">//store answer</span><br />             Session[<span style="color:#a31515">&quot;Captcha&quot;</span> + prefix] = a + b;</p>
<p>             <span style="color:#008000">//image stream</span><br />             <span style="color:#2b91af">FileContentResult</span> img = <span style="color:#0000ff">null</span>;</p>
<p>             <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> mem = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MemoryStream</span>())<br />             <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> bmp = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Bitmap</span>(130, 30))<br />             <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> gfx = <span style="color:#2b91af">Graphics</span>.FromImage((<span style="color:#2b91af">Image</span>)bmp))<br />             {<br />                 gfx.TextRenderingHint = <span style="color:#2b91af">TextRenderingHint</span>.ClearTypeGridFit;<br />                 gfx.SmoothingMode = <span style="color:#2b91af">SmoothingMode</span>.AntiAlias;<br />                 gfx.FillRectangle(<span style="color:#2b91af">Brushes</span>.White, <span style="color:#0000ff">new</span> <span style="color:#2b91af">Rectangle</span>(0, 0, bmp.Width, bmp.Height));</p>
<p>                 <span style="color:#008000">//add noise</span><br />                 <span style="color:#0000ff">if</span> (noisy)<br />                 {<br />                     <span style="color:#0000ff">int</span> i, r, x, y;<br />                     <span style="color:#0000ff">var</span> pen = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Pen</span>(<span style="color:#2b91af">Color</span>.Yellow);<br />                     <span style="color:#0000ff">for</span> (i = 1; i &lt; 10; i++)<br />                     {<br />                         pen.Color = <span style="color:#2b91af">Color</span>.FromArgb(<br />                         (rand.Next(0, 255)),<br />                         (rand.Next(0, 255)),<br />                         (rand.Next(0, 255)));</p>
<p>                         r = rand.Next(0, (130 / 3));<br />                         x = rand.Next(0, 130);<br />                         y = rand.Next(0, 30);</p>
<p>                         gfx.DrawEllipse(pen, x &#8211; r, y &#8211; r, r, r);<br />                     }<br />                 }</p>
<p>                 <span style="color:#008000">//add question</span><br />                 gfx.DrawString(captcha, <span style="color:#0000ff">new</span> <span style="color:#2b91af">Font</span>(<span style="color:#a31515">&quot;Tahoma&quot;</span>, 15), <span style="color:#2b91af">Brushes</span>.Gray, 2, 3);</p>
<p>                 <span style="color:#008000">//render as Jpeg</span><br />                 bmp.Save(mem, System.Drawing.Imaging.<span style="color:#2b91af">ImageFormat</span>.Jpeg);<br />                 img = <span style="color:#0000ff">this</span>.File(mem.GetBuffer(), <span style="color:#a31515">&quot;image/Jpeg&quot;</span>);<br />             }</p>
<p>             <span style="color:#0000ff">return</span> img;<br />         }</div>
</p></div>
</p></div>
<p>If you want to use multiple&#160; CAPTCHAs you can use the prefix to store the answer for each form. Much can be improved regarding the rendered image, for example I could use different font and size for each number in the equation, replace the noise with text distortion.</p>
<h5>Include&#160; CAPTCHA validator in Model and View</h5>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:42a88e72-03cf-4367-a0ef-1aa19b672cde" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Models.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">SubscribeModel</span><br />     {<br />         <span style="color:#008000">//model specific fields</span></p>
<p>         [<span style="color:#2b91af">Required</span>]<br />         [<span style="color:#2b91af">Display</span>(Name = <span style="color:#a31515">&quot;How much is&quot;</span>)]<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Captcha { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }</div>
</p></div>
</p></div>
<p>In the View, beside a label, textbox and validator span you’ll need to add an image placeholder for the CAPTCHA.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5bdf310c-39e2-40f5-b80d-a3feed9ef086" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="background:#ffff00">@*</span><span style="color:#006400">form specific fields</span><span style="background:#ffff00">*@</span></p>
<p> <span style="color:#0000ff">&lt;</span><span style="color:#800000">div</span> <span style="color:#ff0000">class</span><span style="color:#0000ff">=&quot;editor-label&quot;&gt;</span><br />     <span style="background:#ffff00">@</span>Html.LabelFor(model =&gt; model.Captcha)<br />     <span style="color:#0000ff">&lt;</span><span style="color:#800000">a</span> <span style="color:#ff0000">href</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Action(&quot;Index&quot;)&quot;&gt;</span><br />         <span style="color:#0000ff">&lt;</span><span style="color:#800000">img</span> <span style="color:#ff0000">alt</span><span style="color:#0000ff">=&quot;Captcha&quot;</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Action(&quot;CaptchaImage&quot;)&quot;</span> <span style="color:#ff0000">style</span><span style="color:#0000ff">=&quot;&quot;</span> <span style="color:#0000ff">/&gt;</span><br />     <span style="color:#0000ff">&lt;/</span><span style="color:#800000">a</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">div</span> <span style="color:#ff0000">class</span><span style="color:#0000ff">=&quot;editor-field&quot;&gt;</span> <br />     <span style="background:#ffff00">@</span>Html.EditorFor(model =&gt; model.Captcha)<br />     <span style="background:#ffff00">@</span>Html.ValidationMessageFor(model =&gt; model.Captcha)<br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<h5>Validate CAPTCHA on the server side</h5>
<p>Inside your post action where the form submits you can validate the answer by comparing with the session value.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a4a31a40-4787-442e-ae37-90d51f0da429" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">CaptchaController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">[<span style="color:#2b91af">HttpPost</span>]<br /> <span style="color:#0000ff">public</span> <span style="color:#2b91af">ActionResult</span> Index(<span style="color:#2b91af">SubscribeModel</span> model)<br /> {<br />     <span style="color:#008000">//validate captcha</span><br />     <span style="color:#0000ff">if</span> (Session[<span style="color:#a31515">&quot;Captcha&quot;</span>] == <span style="color:#0000ff">null</span> || Session[<span style="color:#a31515">&quot;Captcha&quot;</span>].ToString() != model.Captcha)<br />     {<br />         ModelState.AddModelError(<span style="color:#a31515">&quot;Captcha&quot;</span>, <span style="color:#a31515">&quot;Wrong value of sum, please try again.&quot;</span>);<br />         <span style="color:#008000">//dispay error and generate a new captcha</span><br />         <span style="color:#0000ff">return</span> View(model);<br />     }</p>
<p>     <span style="color:#0000ff">return</span> RedirectToAction(<span style="color:#a31515">&quot;ThankYouPage&quot;</span>);<br /> }</div>
</p></div>
</p></div>
<p>Instead of validating the CAPTCHA on post, you can use an Ajax call to validate and refresh the image asynchronous, more details on how you can send forms data over Ajax read this <a href="http://www.stefanprodan.eu/2011/04/async-operations-with-jquery-ajax-and-asp-net-mvc/">blog post</a>. </p>
<h5>Prerequisites &amp; Download</h5>
<ul>
<li><a href="http://www.microsoft.com/download/en/details.aspx?id=23691" target="_blank">VS.NET 2010 SP1</a> </li>
<li><a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=27419" target="_blank">ASP.NET MVC 4 Developer Preview</a> </li>
</ul>
<p>In order to easily install all the prerequisites you can use the <a href="http://www.microsoft.com/web/">Web Platform Installer</a> version 4.</p>
<p><a href="http://www.stefanprodan.eu/admin/code/CaptchaMvc.zip"><img title="download source code" alt="download source code files" src="http://www.stefanprodan.eu/wp-content/uploads/2011/02/downloadsc.png" width="186" height="22" /></a></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/hLBqjZpDdec" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2012/01/user-friendly-captcha-for-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2012/01/user-friendly-captcha-for-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>A front-end developer specs in the ASP.NET MVC world</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/_7V0KaTrZMo/</link>
		<comments>http://www.stefanprodan.eu/2011/09/front-end-developer-specs/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 00:31:41 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[On the web]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=606</guid>
		<description><![CDATA[I am writing this article because I want to explain the basics of a  front-end developer job at the company I work for.  In my opinion, a front-end developer who...]]></description>
			<content:encoded><![CDATA[<p>I am writing this article because I want to explain the basics of a  front-end developer job at the company I work for.  In my opinion, a front-end developer who wants to be part of an ASP.NET MVC project should be, first of all, well informed regarding the web industry evolution and revolutions. Sometimes the front-end developer acts as a proxy for the final user, this means he or she must have extensive knowledge of usability, user centered design and  information architecture.</p>
<h4><span style="color: #c0504d;">Must have skills</span></h4>
<h5>UI Design –  HTML5</h5>
<p>It’s easy to say ‘I know html’ but in fact building a good html layout that’s simple and compact, ready for ajax updates, that sticks to the standards is a challenging task. A front-end designer should focus on what’s the purpose of a certain front-end interface and how the usage flow should be suggested to the user in terms of usability and ease of access to data. A good HTML layout today doesn’t mean you have to create the whole thing in Photoshop and slice it up,  beside an icons collection everything that’s design related must be written in HTML code and CSS. Sure, you can use Photoshop or Illustrator to sketch the concept design if that’s your tool of choice.</p>
<p>If you are not familiar with HTML5 then you can start with the <a href="http://playground.html5rocks.com/#semantic_markup" target="_blank">Semantic Markup</a>, <a href="http://playground.html5rocks.com/#form_fields" target="_blank">Form Fields</a> and <a href="http://playground.html5rocks.com/#link_relations" target="_blank">Link Relations</a>, what i really recommend is <a href="http://diveintohtml5.org/" target="_blank">Dive into HTML5</a>, a free book by Mark Pilgrim. Oh, and don’t worry about IE not being able to handle  HTML5 markup, ASP.NET MVC uses a JavaScript library called <a href="http://www.modernizr.com/">Modernizr</a> that can make most of  the code compatible with older IE versions.</p>
<h5>UI Design –  CSS3</h5>
<p>CSS3 is an important step to more responsive and intuitive user interfaces, same html code can work nicely on most devices with help from  CSS3’s <a href="https://developer.mozilla.org/En/CSS/Media_queries">media queries</a> and <a href="https://developer.mozilla.org/en/CSS/Pseudo-elements">pseudo-elements</a>, check out more CSS3 new features at <a href="http://www.css3.info/preview/">css3.info</a>.</p>
<p>CSS code tends to get huge when working on a big scale ASP.NET MVC project made for the enterprise market. For that we are using the <a href="http://sass-lang.com/">Sassy CSS extension</a>. It’s odd at the beginning for a web designer but SASS can make a big difference when it comes to  efficiency in front-end developing. Using Visual Studio 2010 with the Web Workbench from <a title="http://www.mindscapehq.com" href="http://www.mindscapehq.com">mindscape</a>,  the SASS code gets easily compiled into CSS, and it’s fully integrated within the IDE (source control and code behind).</p>
<h5>UI Development  –  jQuery Basics</h5>
<p>A front-end developer should be able to deliver a UI design that has all the different views of an interface, like a view mode and an edit mode &#8211; from plain text to edit inline, that involves good knowledge of jQuery  <a href="http://api.jquery.com/category/selectors/">selectors</a>, <a href="http://api.jquery.com/category/effects/">effects</a>, <a href="http://api.jquery.com/category/css/">css</a> and <a href="http://api.jquery.com/category/manipulation/">manipulation</a>.</p>
<h4><span style="color: #4f81bd;">Great to have skills</span></h4>
<h5>JavaScript programming</h5>
<p>Beside the jQuery basics an experience front-end developer should know how to make good use of js scopes, namespaces, prototypal inheritance model, closures and references. You can improve your js skills by reading the <a title="http://bonsaiden.github.com/JavaScript-Garden" href="http://bonsaiden.github.com/JavaScript-Garden">JavaScript-Garden</a> documentation. JSON and AJAX calls should be on the menu too, and for handling ajax request I strongly recommend the <a href="http://amplifyjs.com/api/request/" target="_blank">Amplify.js</a> library.</p>
<h5>Knowing the patterns</h5>
<p>A front-end developer should understand the Model-View-Controller pattern (<a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank">Wikipedia article</a>) and the .NET implementation (<a href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC3</a>), also the Publish/Subscribe messaging pattern (<a href="http://en.wikipedia.org/wiki/Publish/subscribe" target="_blank">Wikipedia  article</a>) and the JS implementation (<a href="http://amplifyjs.com/api/pubsub/" target="_blank">Amplify.js</a>).</p>
<h5>Database architecture</h5>
<p>A front-end developer should understand a <a href="http://msdn.microsoft.com/en-us/library/ms171971.aspx" target="_blank">database diagram</a> and the sql <a href="http://msdn.microsoft.com/en-us/library/ms187752.aspx" target="_blank">data types</a> in such a way that he can choose the right UI form elements for an ‘edit view’ or to be able to present the information in a logical way for the user &#8211; easy to read and understand.</p>
<h5>Object-Oriented programming</h5>
<p>Any OOP language will do, if you understand C++ or Java it will be easy to read C#. Lambda and regular expressions are always needed in programming; so, at some point you’ll have to learn them.</p>
<h5>Any experience with the IDE</h5>
<p>The main tool used to develop, test and deploy is of course the best programming IDE in the world: <a href="http://www.microsoft.com/visualstudio/" target="_blank">Visual Studio</a>.  Visual Studio can validate your HTLM5 and CSS3 code, can help you with <a href="http://www.stefanprodan.eu/2011/05/improve-visual-studio-2010-jscript-editor-with-jquery-intellisense-and-code-outlining/" target="_blank">JQuery Intellisense</a> and <a href="http://www.mindscapehq.com" target="_blank">Sass</a>.  Any developer must work using source control and in VS.NET 2010 the source control  add-on named <a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/team-foundation-server/overview" target="_blank">TFS</a> is intuitive and easy to use.</p>
<p><strong>If you are looking for a job in Bucharest and you have these skills please <a title="Contact" href="http://www.stefanprodan.eu/contact/">contact</a> me.</strong></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/_7V0KaTrZMo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/09/front-end-developer-specs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/09/front-end-developer-specs/</feedburner:origLink></item>
		<item>
		<title>RSS capabilities in Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/nrpV2b1AiKA/</link>
		<comments>http://www.stefanprodan.eu/2011/07/rss-capabilities-in-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 22:35:51 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=591</guid>
		<description><![CDATA[Exposing a RSS feed on a Asp.Net MVC website can be easily done using the FileResult base class and&#160; Windows Communication Foundation SyndicatedFeed. Write RSS feeds In order to use...]]></description>
			<content:encoded><![CDATA[<p>Exposing a RSS feed on a Asp.Net MVC website can be easily done using the FileResult base class and&#160; Windows Communication Foundation SyndicatedFeed.</p>
<p style="text-align: center"><img class="size-full wp-image-592 aligncenter" title="post-to-wall-rss" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/07/post-to-wall-rss.png" width="582" height="662" /></p>
<h5>Write RSS feeds</h5>
<p>In order to use the WCF SyndicatedFeed first you’ll have to add a reference in your MVC project to the System.ServiceModel assembly, then create a new class derived from&#160; FileResult&#160; like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d072d909-f411-4dc5-aebd-6fbc2aa1e28a" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">FeedResult.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">enum</span> <span style="color:#2b91af">FeedType</span><br />     {<br />         Rss,<br />         Atom<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">FeedResult</span> : <span style="color:#2b91af">FileResult</span><br />     {<br />         <span style="color:#0000ff">private</span> <span style="color:#0000ff">readonly</span> <span style="color:#2b91af">SyndicationFeed</span> feed;<br />         <span style="color:#0000ff">private</span> <span style="color:#2b91af">FeedType</span> type;</p>
<p>         <span style="color:#0000ff">public</span> FeedResult(<span style="color:#2b91af">SyndicationFeed</span> feed, <span style="color:#2b91af">FeedType</span> type)<br />             : <span style="color:#0000ff">base</span>(<span style="color:#a31515">&quot;application/rss+xml&quot;</span>)<br />         {<br />             <span style="color:#0000ff">this</span>.feed = feed;<br />             <span style="color:#0000ff">this</span>.type = type;<br />         }</p>
<p>         <span style="color:#0000ff">protected</span> <span style="color:#0000ff">override</span> <span style="color:#0000ff">void</span> WriteFile(<span style="color:#2b91af">HttpResponseBase</span> response)<br />         {<br />             <span style="color:#0000ff">using</span> (<span style="color:#2b91af">XmlWriter</span> writer = <span style="color:#2b91af">XmlWriter</span>.Create(response.OutputStream))<br />             {<br />                 <span style="color:#0000ff">switch</span> (<span style="color:#0000ff">this</span>.type)<br />                 {<br />                     <span style="color:#0000ff">case</span> <span style="color:#2b91af">FeedType</span>.Rss:<br />                         <span style="color:#0000ff">this</span>.feed.GetRss20Formatter().WriteTo(writer);<br />                         <span style="color:#0000ff">break</span>;<br />                     <span style="color:#0000ff">case</span> <span style="color:#2b91af">FeedType</span>.Atom:<br />                         <span style="color:#0000ff">this</span>.feed.GetAtom10Formatter().WriteTo(writer);<br />                         <span style="color:#0000ff">break</span>;<br />                 }<br />             }<br />         }<br />     }</div>
</p></div>
</p></div>
<p>Next I will show you how I’ve add a RSS 2.0 Feed to my Post-To-Wall app that will fetch the latest ten notes from the database and create for each one a valid SyndicatedItem.&#160; On the main controller I’ve added a new action that will return a FeedResult like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c40abd81-d671-47fa-b299-1cfa62b89c6e" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">TextNotesController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#2b91af">FileResult</span> Rss2Feed()<br /> {<br />     <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">SyndicationItem</span>&gt; rssItems = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">SyndicationItem</span>&gt;();<br />     <span style="color:#2b91af">SyndicationFeed</span> feed =<br />         <span style="color:#0000ff">new</span> <span style="color:#2b91af">SyndicationFeed</span>(<span style="color:#a31515">&quot;My Web Notes&quot;</span>,<br />                             <span style="color:#a31515">&quot;Post To Wall app&quot;</span>,<br />                             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Uri</span>(Request.Url.ToString()),<br />                             <span style="color:#a31515">&quot;ptwID&quot;</span>,<br />                             <span style="color:#2b91af">DateTime</span>.Now);<br />     <span style="color:#0000ff">var</span> list = (<span style="color:#0000ff">from</span> x <span style="color:#0000ff">in</span> db.TxtNotes <span style="color:#0000ff">orderby</span> x.Created <span style="color:#0000ff">descending</span> <span style="color:#0000ff">select</span> x).Take(10).ToList();<br />     <span style="color:#0000ff">var</span> url = Request.Url.AbsoluteUri.Replace(<span style="color:#a31515">&quot;Rss2Feed&quot;</span>, <span style="color:#0000ff">string</span>.Empty);<br />     <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> item <span style="color:#0000ff">in</span> list)<br />     {<br />         <span style="color:#008000">//shorten title to 5 words</span><br />         <span style="color:#0000ff">var</span> shortTitle = item.Title.Split(<span style="color:#a31515">&#39; &#39;</span>).Take(5).Aggregate((x, y) =&gt; x + <span style="color:#a31515">&quot; &quot;</span> + y);<br />         <span style="color:#0000ff">var</span> rssItem = <span style="color:#0000ff">new</span> <span style="color:#2b91af">SyndicationItem</span>(shortTitle,<br />             item.Title,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Uri</span>(url + <span style="color:#a31515">&quot;ViewNote/&quot;</span> + item.Id),<br />             item.Id.ToString(),<br />             item.Created.Value);<br />         rssItems.Add(rssItem);<br />     }</p>
<p>     feed.Items = rssItems;</p>
<p>     <span style="color:#0000ff">return</span> <span style="color:#0000ff">new</span> <span style="color:#2b91af">FeedResult</span>(feed, <span style="color:#2b91af">FeedType</span>.Rss);<br /> }</div>
</p></div>
</p></div>
<p>Now that we have the RSS action ready we can expose it in the HTML Head tag so any browser can auto-detect the link:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d93a2d9a-462d-4db8-b827-1a4b7e757da6" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">_NotesLayout.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;</span><span style="color:#800000">link</span> <span style="color:#ff0000">rel</span><span style="color:#0000ff">=&quot;alternate&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;application/rss+xml&quot;</span> <span style="color:#ff0000">title</span><span style="color:#0000ff">=&quot;RSS&quot;</span> <span style="color:#ff0000">href</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Action(</span><span style="color:#a31515">&quot;Rss2Feed&quot;</span><span style="color:#0000ff">, </span><span style="color:#a31515">&quot;TextNotes&quot;</span><span style="color:#0000ff">, null, </span><span style="color:#a31515">&quot;http&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#0000ff">/&gt;</span></div>
</p></div>
</p></div>
<p>Note that I using URL helper with http as protocol, this way it will generate the absolute url not a relative one.</p>
<h5>Read RSS Feeds using an AsyncController</h5>
<p>In Post-To-Wall app I’ve added a new page that shows updates from my blog by displaying the blog’s RSS feed. Because the server needs to download the xml file from my blog I’ve decided to use an AsyncController so I can avoid blocking the IIS server if the fetching takes time.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d51ea28d-37b7-4f9b-8405-52344efed9c7" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">RssReaderAsyncController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">RssReaderAsyncController</span> : <span style="color:#2b91af">AsyncController</span><br /> { <br />     <span style="color:#0000ff">readonly</span> <span style="color:#0000ff">object</span> locker = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Object</span>();</p>
<p>     [<span style="color:#2b91af">OutputCache</span>(Duration = 60 * 10 <span style="color:#008000">/* 10min */</span>, VaryByParam = <span style="color:#a31515">&quot;url&quot;</span>)]<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> GetFeedAsync(<span style="color:#0000ff">string</span> url)<br />     {<br />         url = <span style="color:#0000ff">string</span>.IsNullOrEmpty(url) ? <span style="color:#a31515">&quot;http://www.stefanprodan.eu/?feed=rss2&quot;</span> : url;<br />         AsyncManager.OutstandingOperations.Increment(1);<br />         <span style="color:#0000ff">var</span> req = <span style="color:#2b91af">WebRequest</span>.Create(url);<br />         req.BeginGetResponse(result =&gt;<br />         {<br />             <span style="color:#0000ff">var</span> webResponse = req.EndGetResponse(result);<br />             <span style="color:#2b91af">XmlReader</span> reader = <span style="color:#2b91af">XmlReader</span>.Create(webResponse.GetResponseStream());<br />             <span style="color:#2b91af">SyndicationFeed</span> feed = <span style="color:#2b91af">SyndicationFeed</span>.Load(reader);</p>
<p>             <span style="color:#008000">//thread safe</span><br />             <span style="color:#0000ff">lock</span> (locker)<br />             {<br />                 AsyncManager.Parameters[<span style="color:#a31515">&quot;feed&quot;</span>] = feed;<br />             }<br />             AsyncManager.OutstandingOperations.Decrement();</p>
<p>         }, <span style="color:#0000ff">null</span>);<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#2b91af">ActionResult</span> GetFeedCompleted(<span style="color:#2b91af">SyndicationFeed</span> feed)<br />     {<br />         <span style="color:#0000ff">return</span> View(<span style="color:#a31515">&quot;Feed&quot;</span>, feed);<br />     }<br /> }</div>
</p></div>
</p></div>
<p>If you want to cache an AsyncController’s action you have to add the OutputCache attribute on the Async method. In order to get the Feed view, on the url you’ll have to specify the action name without Async or Completed, just GetFeed:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3b81e56c-f05f-4a1e-8563-888294bb5911" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="background:#ffff00">@</span>Html.ActionLink(<span style="color:#a31515">&quot;Updates&quot;</span>, <span style="color:#a31515">&quot;GetFeed&quot;</span>, <span style="color:#a31515">&quot;RssReaderAsync&quot;</span>)</div>
</p></div>
</p></div>
<h6>Download project source code</h6>
<p>Before running the project make sure you have VS.NET 2010 SP1, MVC 3 Tool Update and IIS 7.5 Express installed.</p>
<p><a href="http://www.stefanprodan.eu/admin/code/Mvc-Post-to-Wall.zip"><img title="download source code" alt="download source code files" src="http://www.stefanprodan.eu/wp-content/uploads/2011/02/downloadsc.png" width="186" height="22" /></a></p>
<h6>Post-to-Wall app previous posts</h6>
<p><a href="http://www.stefanprodan.eu/2011/06/load-on-demand-and-session-sync-with-jquery-and-asp-net-mvc/">Load on demand and Session Sync with JQuery and Asp.Net MVC</a>    <br /><a href="http://www.stefanprodan.eu/2011/05/edit-data-in-dialog-form-with-jquery-and-asp-net-mvc/">Edit data in dialog form with JQuery and Asp.Net MVC</a>    <br /><a href="http://www.stefanprodan.eu/2011/04/async-operations-with-jquery-ajax-and-asp-net-mvc/">Async operations with jQuery Ajax and Asp.Net MVC</a>    <br /><a href="http://www.stefanprodan.eu/2011/04/starting-a-new-app-with-asp-net-mvc-3-jquery-ui/">Starting a new app with Asp.Net MVC 3 &amp; JQuery UI</a></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/nrpV2b1AiKA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/07/rss-capabilities-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/07/rss-capabilities-in-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>Load on demand and Session Sync with JQuery and Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/AO_wI8B6rKE/</link>
		<comments>http://www.stefanprodan.eu/2011/06/load-on-demand-and-session-sync-with-jquery-and-asp-net-mvc/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 11:54:02 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=550</guid>
		<description><![CDATA[In this post I will show you how to make a load on demand link that will fetch older entries from the database and how to synchronize newly added entries...]]></description>
			<content:encoded><![CDATA[<p>In this post I will show you how to make a load on demand link that will fetch older entries from the database and how to synchronize newly added entries across multiple browser sessions using a JQuery timer.</p>
<h5>Load on demand</h5>
<p style="text-align: center"><img class="size-full wp-image-561 aligncenter" title="mvc-load-on-demand" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/06/mvc-load-on-demand.png" width="591" height="471" /></p>
<p>In order to show the “load older entries” link on the page we need to know if in the database there are more notes then displayed. For this feature to work, I’ve modified the <em>GetNotes</em> action to return a json parameter named <em>IsMore,</em> every time the user clicks on this link an Ajax call is made to the server that will fetch five more notes or will hide the link if there are no more notes to display:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:688c8cb7-4376-4a47-b2c8-4c858d0ab7ef" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">TextNotesController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">[<span style="color:#2b91af">HttpPost</span>]<br /> <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> GetOlderNotes(<span style="color:#0000ff">int</span> id, <span style="color:#0000ff">int</span> count = 5)<br /> {<br />     <span style="color:#0000ff">string</span> message = <span style="color:#0000ff">string</span>.Empty;<br />     <span style="color:#0000ff">bool</span> isMore = <span style="color:#0000ff">true</span>;<br />     <span style="color:#0000ff">var</span> list = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">TxtNote</span>&gt;();<br />     <span style="color:#0000ff">try</span><br />     {<br />         list = (<span style="color:#0000ff">from</span> x <span style="color:#0000ff">in</span> db.TxtNotes <span style="color:#0000ff">where</span> x.Id &lt; id <span style="color:#0000ff">orderby</span> x.Created <span style="color:#0000ff">descending</span> <span style="color:#0000ff">select</span> x).Take(count).ToList();<br />         isMore = (<span style="color:#0000ff">from</span> x <span style="color:#0000ff">in</span> db.TxtNotes <span style="color:#0000ff">where</span> x.Id &lt; id <span style="color:#0000ff">select</span> x.Id).Take(count + 1).Count() &#8211; count &gt; 0;<br />     }<br />     <span style="color:#0000ff">catch</span> (<span style="color:#2b91af">Exception</span> ex)<br />     {<br />         message = ex.Message;<br />     }</p>
<p>     <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span><br />     {<br />         Html = <span style="color:#0000ff">this</span>.RenderPartialView(<span style="color:#a31515">&quot;_NotesList&quot;</span>, list),<br />         IsMore = isMore,<br />         Message = message<br />     }, <span style="color:#2b91af">JsonRequestBehavior</span>.AllowGet);<br /> }</div>
</p></div>
</p></div>
<p>The JQuery code that handles the click event will look for the oldest note ID displayed and calls the above action, if the interface is in search mode then it will call a similar action that will fetch more results.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e7d406a9-5145-4e19-bcbb-a04a14e15236" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#006400">//load more results</span><br /> $(<span style="color:#0000ff">function</span> () {<br />     $(<span style="color:#800000">&quot;#loadOlder&quot;</span>).click(<span style="color:#0000ff">function</span> (e) {<br />         e.preventDefault();<br />         <span style="color:#006400">//find oldest id displayed</span><br />         <span style="color:#0000ff">var</span> Id = $(<span style="color:#800000">&quot;ol#update li.item:last&quot;</span>);<br />         <span style="color:#0000ff">if</span> (Id[0]) {<br />             Id = Id.attr(<span style="color:#800000">&quot;id&quot;</span>).replace(<span style="color:#800000">&quot;bar-&quot;</span>, <span style="color:#800000">&quot;&quot;</span>);<br />         } <span style="color:#0000ff">else</span> {<br />             Id = 0;<br />         }<br />         <span style="color:#006400">//load more search results</span><br />         <span style="color:#0000ff">if</span> (searchMode) {<br />             <span style="color:#0000ff">var</span> boxval = $(<span style="color:#800000">&quot;#content&quot;</span>).val();<br />             <span style="color:#0000ff">if</span> (boxval == <span style="color:#800000">&#39;&#39;</span>) {<br />                 showError(<span style="color:#800000">&quot;You must type something before searching&quot;</span>);<br />             } <span style="color:#0000ff">else</span> {<br />                 $.ajax({<br />                     type: <span style="color:#800000">&quot;POST&quot;</span>,<br />                     url: <span style="color:#800000">&quot;TextNotes/SearchOlderNotes&quot;</span>,<br />                     data: { keyword: boxval, id: Id, count: defaultCount },<br />                     cache: <span style="color:#0000ff">false</span>,<br />                     dataType: <span style="color:#800000">&quot;json&quot;</span>,<br />                     success: <span style="color:#0000ff">function</span> (data) {<br />                         date = <span style="color:#0000ff">new</span> Date();<br />                         <span style="color:#0000ff">if</span> (data.Html) {<br />                             $(<span style="color:#800000">&quot;ol#update&quot;</span>).append(data.Html);<br />                             $(<span style="color:#800000">&quot;ol#update li&quot;</span>).slideDown(<span style="color:#800000">&quot;slow&quot;</span>);<br />                             <span style="color:#0000ff">if</span> (data.IsMore) {<br />                                 $(<span style="color:#800000">&quot;ol#footer li&quot;</span>).slideDown(<span style="color:#800000">&quot;slow&quot;</span>);<br />                                 $(<span style="color:#800000">&quot;#loadOlder&quot;</span>).show();<br />                             } <span style="color:#0000ff">else</span> {<br />                                 $(<span style="color:#800000">&quot;#loadOlder&quot;</span>).hide();<br />                             }<br />                         } <span style="color:#0000ff">else</span> {<br />                             showError(data.Message);<br />                         }<br />                         $(<span style="color:#800000">&quot;#loaderAnim&quot;</span>).hide();<br />                     }<br />                 });<br />             }<br />         <span style="color:#006400">//load older notes</span><br />         } <span style="color:#0000ff">else</span> {<br />             $.ajax({<br />                 type: <span style="color:#800000">&quot;POST&quot;</span>,<br />                 url: <span style="color:#800000">&quot;TextNotes/GetOlderNotes&quot;</span>,<br />                 data: { id: Id, count: defaultCount },<br />                 cache: <span style="color:#0000ff">false</span>,<br />                 dataType: <span style="color:#800000">&quot;json&quot;</span>,<br />                 success: <span style="color:#0000ff">function</span> (data) {<br />                     date = <span style="color:#0000ff">new</span> Date();<br />                     <span style="color:#0000ff">if</span> (data.Html) {<br />                         $(<span style="color:#800000">&quot;ol#update&quot;</span>).append(data.Html);<br />                         $(<span style="color:#800000">&quot;ol#update li&quot;</span>).slideDown(<span style="color:#800000">&quot;slow&quot;</span>);<br />                         <span style="color:#0000ff">if</span> (!data.IsMore) {<br />                             <span style="color:#006400">//hide loader if there are no more notes</span><br />                             $(<span style="color:#800000">&quot;#loadOlder&quot;</span>).hide();<br />                         }<br />                     } <span style="color:#0000ff">else</span> {<br />                         showError(data.Message);<br />                     }<br />                     $(<span style="color:#800000">&quot;#loaderAnim&quot;</span>).hide();<br />                 }<br />             });<br />         }<br />         <span style="color:#0000ff">return</span> <span style="color:#0000ff">false</span>;<br />     });<br /> });</div>
</p></div>
</p></div>
<h5>Synchronization across multiple sessions</h5>
<p>I’ve added a background thread (JQuery Timer) that will check if there are any recent notes that have been inserted from another browser session.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:84f7978d-ff7e-43d7-aad1-46f5596d9e93" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> threadId = 1;<br /> <span style="color:#0000ff">var</span> date = <span style="color:#0000ff">new</span> Date();</p>
<p> $(document).ready(<span style="color:#0000ff">function</span> () {</p>
<p>     <span style="color:#006400">//lunch sync thread</span><br />     threadId = setInterval(<span style="color:#0000ff">function</span> () {<br />         loadNotes();<br />     }, 15000); <span style="color:#006400">//thread sleep 15 sec</span><br /> });</div>
</p></div>
</p></div>
<p>Every 15 seconds the timer will make an Ajax call to the <em>GetLatestNotes</em> action using the latest note ID inserted and date:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d5082eb5-31cd-46bd-ae80-e7ffdf7aba7f" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">TextNotesController.cs</div>
<div style="background-color: #ffffff; max-height: 500px; overflow: auto; padding: 2px 5px;">[<span style="color:#2b91af">HttpPost</span>]<br /> <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> GetLatestNotes(<span style="color:#0000ff">int</span> id, <span style="color:#2b91af">DateTime</span>? lastDate)<br /> {<br />     <span style="color:#0000ff">string</span> message = <span style="color:#0000ff">string</span>.Empty;<br />     <span style="color:#0000ff">var</span> list = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">TxtNote</span>&gt;();<br />     <span style="color:#0000ff">try</span><br />     {<br />         <span style="color:#0000ff">if</span> (lastDate.HasValue)<br />             list = (<span style="color:#0000ff">from</span> x <span style="color:#0000ff">in</span> db.TxtNotes <span style="color:#0000ff">where</span> x.Created &gt; lastDate.Value <span style="color:#0000ff">orderby</span> x.Created <span style="color:#0000ff">descending</span> <span style="color:#0000ff">select</span> x).ToList();<br />         <span style="color:#0000ff">else</span><br />             list = (<span style="color:#0000ff">from</span> x <span style="color:#0000ff">in</span> db.TxtNotes <span style="color:#0000ff">where</span> x.Id &gt; id <span style="color:#0000ff">orderby</span> x.Created <span style="color:#0000ff">descending</span> <span style="color:#0000ff">select</span> x).ToList();<br />     }<br />     <span style="color:#0000ff">catch</span> (<span style="color:#2b91af">Exception</span> ex)<br />     {<br />         message = ex.Message;<br />     }<br />             <br />     <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span><br />     {<br />         Html = <span style="color:#0000ff">this</span>.RenderPartialView(<span style="color:#a31515">&quot;_NotesList&quot;</span>, list),<br />         Message = message<br />     }, <span style="color:#2b91af">JsonRequestBehavior</span>.AllowGet);<br /> }</div>
</p></div>
</p></div>
<p>If the server gets offline the timer will stop making calls :</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4891bed5-b9d6-4137-baf1-95eccff0d017" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">if</span> (searchMode) <span style="color:#0000ff">return</span>;<br /> <span style="color:#006400">//sync notes with other sessions</span><br /> $(<span style="color:#800000">&quot;#loaderAnim&quot;</span>).show();<br /> <span style="color:#0000ff">var</span> Id = $(<span style="color:#800000">&quot;ol#update li:first&quot;</span>);<br /> <span style="color:#0000ff">if</span> (Id[0]) { <span style="color:#006400">//get last inserted id on this session </span><br />     Id = Id.attr(<span style="color:#800000">&quot;id&quot;</span>).replace(<span style="color:#800000">&quot;bar-&quot;</span>, <span style="color:#800000">&quot;&quot;</span>);<br /> } <span style="color:#0000ff">else</span> {<br />     Id = 0;<br /> }<br /> $.ajax({<br />     type: <span style="color:#800000">&quot;POST&quot;</span>,<br />     url: <span style="color:#800000">&quot;TextNotes/GetLatestNotes&quot;</span>,<br />     data: { id: Id, lastDate: dateFormat(date, <span style="color:#800000">&#39;isoUtcDateTime&#39;</span>) },<br />     cache: <span style="color:#0000ff">false</span>,<br />     dataType: <span style="color:#800000">&quot;json&quot;</span>,<br />     success: <span style="color:#0000ff">function</span> (data) {<br />         date = <span style="color:#0000ff">new</span> Date();<br />         <span style="color:#0000ff">if</span> (data.Html) { <span style="color:#006400">//show latest notes</span><br />             $(<span style="color:#800000">&quot;ol#update&quot;</span>).prepend(data.Html);<br />             $(<span style="color:#800000">&quot;ol#update li&quot;</span>).slideDown(<span style="color:#800000">&quot;slow&quot;</span>);<br />         }<br />         $(<span style="color:#800000">&quot;#loaderAnim&quot;</span>).hide();<br />     },<br />     error: <span style="color:#0000ff">function</span> () {<br />         <span style="color:#006400">//stop thread &#8211; server is offline</span><br />         clearInterval(threadId);<br />         $(<span style="color:#800000">&quot;#loaderAnim&quot;</span>).hide();<br />     }<br /> });</div>
</p></div>
</p></div>
<p>This way I try to keep notes synchronized on all browsers that the user may be using at the same time, or are opened on different devices.</p>
<h5>Download project source code</h5>
<p>Before running the project make sure you have VS.NET 2010 SP1, MVC 3 Tool Update and IIS 7.5 Express installed.</p>
<p><a href="http://www.stefanprodan.eu/admin/code/Mvc-Post-to-Wall.zip"><img class="size-full wp-image-104" title="download source code" alt="download source code files" src="http://www.stefanprodan.eu/wp-content/uploads/2011/02/downloadsc.png" width="186" height="22" /></a></p>
<h5>Post-to-Wall app previous posts</h5>
<p><a href="http://www.stefanprodan.eu/2011/05/edit-data-in-dialog-form-with-jquery-and-asp-net-mvc/">Edit data in dialog form with JQuery and Asp.Net MVC</a>     <br /><a title="Async operations with jQuery Ajax and Asp.Net MVC" href="http://www.stefanprodan.eu/2011/04/async-operations-with-jquery-ajax-and-asp-net-mvc/">Async operations with jQuery Ajax and Asp.Net MVC</a>     <br /><a href="http://www.stefanprodan.eu/2011/04/starting-a-new-app-with-asp-net-mvc-3-jquery-ui/">Starting a new app with Asp.Net MVC 3 &amp; JQuery UI</a></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/AO_wI8B6rKE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/06/load-on-demand-and-session-sync-with-jquery-and-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/06/load-on-demand-and-session-sync-with-jquery-and-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>WYSIWYG HTML Editors for Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/NNmUigmvXxQ/</link>
		<comments>http://www.stefanprodan.eu/2011/06/wysiwyg-html-editors-for-asp-net-mvc/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 21:12:06 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=535</guid>
		<description><![CDATA[I was looking for a good What-You-See-Is-What-You-Get HTML editor that works on the Asp.Net MVC platform, after extensive searching and testing I came up with two solutions: CLEditor as a...]]></description>
			<content:encoded><![CDATA[<p>I was looking for a good What-You-See-Is-What-You-Get HTML editor that works on the Asp.Net MVC platform, after extensive searching and testing I came up with two solutions: CLEditor as a lightweight ultrafast editor, ideal for basic text operations and TinyMCE as the full featured editor suitable for CMS applications. Both editors are cross browser(IE 6.0+, FF, Safari, Chrome, Opera) and can be used as a JQuery plugin.</p>
<h5>CLEditor</h5>
<p>CLEditor is an open source jQuery plugin that has the standard text formatting features plus text color and highlight color.&#160; It’s a perfect replacement for the textarea control if you need basic text editing, CLEditor renders fast and has only 16KB minified(9KB gzip). The plugin is dual licensed MIT and GPL, that means that it can be used in any kind of application, open or closed source.</p>
<p>You can add CLEditor to your MVC 3 project using <a href="http://nuget.org/List/Packages/CLEditor" target="_blank">this NuGet package</a> or by downloading from the <a href="http://premiumsoftware.net/cleditor/index.html" target="_blank">plugin website</a>.&#160; If you want CLEditor to produce Transitional XHTML compliant source, download this <a href="http://premiumsoftware.net/cleditor/downloads/CLEditor.XHTML1_0_0.zip" target="_blank">js file</a>.</p>
<p><a href="http://premiumsoftware.net/cleditor/index.html"><img class="alignnone size-full wp-image-536" title="CLEditor" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/06/CLEditor.png" width="513" height="264" /></a></p>
<p>Using CLEditor with Asp.Net MVC 3 and JQuery 1.6.1</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f79b3c43-011b-46c5-b46e-96ad9bee5265" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">textarea</span> <span style="color:#ff0000">id</span><span style="color:#0000ff">=&quot;input&quot;</span> <span style="color:#ff0000">name</span><span style="color:#0000ff">=&quot;input&quot;&gt;&lt;/</span><span style="color:#800000">textarea</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span></p>
<p> <span style="color:#0000ff">&lt;</span><span style="color:#800000">link</span> <span style="color:#ff0000">href</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Content(</span><span style="color:#a31515">&quot;~/Scripts/cleditor/jquery.cleditor.css&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#ff0000">rel</span><span style="color:#0000ff">=&quot;stylesheet&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/css&quot;</span> <span style="color:#0000ff">/&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Content(</span><span style="color:#a31515">&quot;~/Scripts/cleditor/jquery.cleditor.min.js&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Content(</span><span style="color:#a31515">&quot;~/Scripts/cleditor/jquery.cleditor.xhtml.min.js&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;</span><br />     $(<span style="color:#800000">&quot;#input&quot;</span>).cleditor();<br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>CLEditor will synchronize the editor’s xhtml content with the textarea value, meaning that jquery form serialization over ajax will work just fine. You can easily update the editor by calling updateFrame() function after changing the textarea&#160; value.</p>
<h5>TinyMCE</h5>
<p>TinyMCE is one of the best JavaScript powered word processor on the web and it’s used by millions of users in Facebook, WordPress, Joomla, Umbraco and many more. In order to use it in Asp.Net MVC you’ll have to download the special jQuery build of TinyMCE and the jQuery integration plugin from the <a href="http://tinymce.moxiecode.com/download/download.php" target="_blank">official website</a>.</p>
<p><a href="http://tinymce.moxiecode.com/"><img class="alignnone size-full wp-image-537" title="TinyMCE" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/06/TinyMCE.png" width="622" height="210" /></a></p>
<p>Using TinyMCE with Asp.Net MVC 3</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fbcfa963-900e-48ef-8d03-6455b0ec0a92" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">textarea</span> <span style="color:#ff0000">id</span><span style="color:#0000ff">=&quot;editor&quot;</span> <span style="color:#ff0000">name</span><span style="color:#0000ff">=&quot;editor&quot;&gt;&lt;/</span><span style="color:#800000">textarea</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">div</span><span style="color:#0000ff">&gt;</span></p>
<p> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Content(</span><span style="color:#a31515">&quot;~/Scripts/tiny_mce/tiny_mce.js&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;</span><br />     tinyMCE.init({<br />         mode: <span style="color:#800000">&quot;exact&quot;</span>,<br />         theme: <span style="color:#800000">&quot;advanced&quot;</span><br />     });<br />     tinyMCE.execCommand(<span style="color:#800000">&#39;mceAddControl&#39;</span>, <span style="color:#0000ff">false</span>, <span style="color:#800000">&#39;editor&#39;</span>);<br /> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>In order to retrieve the editor HTML content you need to call <em><span style="color: #800000">tinyMCE.get(‘editor’).getContent() </span></em>function.</p>
<p>If you want to enable file upload in TinyMCE checkout this project on codeplex: <a href="http://tinymcefckfilemanger.codeplex.com/" target="_blank">TinyMCE with FCKEditor File Upload Manager in ASP.NET MVC 3</a>.</p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/NNmUigmvXxQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/06/wysiwyg-html-editors-for-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/06/wysiwyg-html-editors-for-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>What kind of programming will be suitable for Windows 8?</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/yt_N_cjr2dk/</link>
		<comments>http://www.stefanprodan.eu/2011/06/what-kind-of-programming-will-be-suitable-for-windows-8/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 21:42:45 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Archive]]></category>
		<category><![CDATA[windows 8]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=528</guid>
		<description><![CDATA[There are allot of discussions these days about Microsoft pushing forward with HTML5 and JavaScript as the way to code apps for the new Windows 8 interface. The community is...]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-529" title="Windows 8" src="http://www.stefanprodan.eu/wp-content/uploads/2011/06/windows8.png" alt="Windows 8" width="195" height="110" />There are allot of discussions these days about Microsoft pushing forward with HTML5 and JavaScript as the way to code apps for the new Windows 8 interface. The community is confused because everybody was expecting XAML and C# to be mentioned, or WPF and Silverlight, or even a word on the Jupiter project(native Silverlight apps). I am sure that Microsoft will talk about all these technologies in September at the BUILD conference.</p>
<p>I bet all the existing apps that are working rite now on Windows 7 x64 will also run on Windows 8, they will not have a tile in the touch home screen but will still be accessible. I don’t expect a CRM written in Windows Forms 2.0 to be handled by touch, but a mouse or a pen will do and I am quite certain that  the classic desktop PC will stick around for some time at least in the business sector.</p>
<h5>Programming in 2012?</h5>
<p>I see myself developing applications for the enterprise market using <strong>Asp.Net MVC</strong> v.next with <strong>HTML5</strong> and JavaScript, I think that MVC 3 is mature enough and with the help of <strong>JQuery</strong> and <strong>JQuery Mobile</strong> I can produce reliable complex web apps. Of course I will try to make the most of my Windows 8 tile space, or even coding a <strong>WPF</strong> client to handle time critical notifications and instant messaging.</p>
<p>I see myself in 2012 creating apps for smartphones and pads using <strong>Silverlight</strong> v.next on the client side and WCF hosted by Windows <strong>Azure</strong> on the server side.</p>
<h5>What I expect from Microsoft in 2012?</h5>
<p>First of all I expect that developers from Romania to be allowed to create accounts on Windows 8 app store, I hate to say it but I’ve quit developing with Silverlight because I can’t deploy my apps legally on Wp7 since, as a Romanian, I can’t register on the App Hub site.</p>
<p>Second I expect that WPF will finally become ready for enterprise applications like Windows Forms 2.0 was, and if it’s possible a merge between Silverlight and WPF or at least a way to compile the same code for both technologies. With HTML5 and JQuery on the horizon I can’t imagine why Microsoft wants to invest both in WPF and Silverlight. Maybe the Jupiter project will deliver a solid XAML designer with C# in the code behind and there will be no more need for WPF or the current version of Silverlight and it’s limitations. I really hope that Jupiter, the new XAML powered interface, will work on Windows 7.</p>
<h5>References</h5>
<p>Windows 8 official preview on YouTube<br />
<a href="http://www.youtube.com/watch?v=p92QfWOw88I" target="_blank">Building &#8220;Windows 8&#8243; &#8211; Video #1</a></p>
<p>BUILD Conference<br />
<a href="http://www.buildwindows.com/" target="_blank">Build Windows &#8211; 13 September 2011</a></p>
<p><a href="http://www.youtube.com/watch?v=p92QfWOw88I" target="_blank"></a>Silverlight Forum discussions<br />
<a href="http://forums.silverlight.net/forums/p/230744/563049.aspx" target="_blank">Open Plea by Silverlight / WPF Devs for Full Windows 8 Support in Addition to HTML5</a></p>
<p><a href="http://forums.silverlight.net/forums/p/230744/563049.aspx" target="_blank"></a>Mary-Jo Foley on Jupiter Project<br />
<a href="http://www.zdnet.com/blog/microsoft/microsoft-needs-to-tell-windows-8-developers-now-about-jupiter-and-silverlight/9608?tag=content;feature-roto" target="_blank">Microsoft needs to tell Windows 8 developers now about &#8216;Jupiter&#8217; and Silverlight</a></p>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/yt_N_cjr2dk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/06/what-kind-of-programming-will-be-suitable-for-windows-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/06/what-kind-of-programming-will-be-suitable-for-windows-8/</feedburner:origLink></item>
		<item>
		<title>Improve Visual Studio with JQuery Intellisense and Code Outlining</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/SJgOqGVB04Y/</link>
		<comments>http://www.stefanprodan.eu/2011/05/improve-visual-studio-2010-jscript-editor-with-jquery-intellisense-and-code-outlining/#comments</comments>
		<pubDate>Sat, 28 May 2011 22:02:05 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=519</guid>
		<description><![CDATA[Every .NET programmers is used to brace matching, code collapsing and Intellisense because Visual Studio got them from the beginning, I expect to have these helpers when I write jscript...]]></description>
			<content:encoded><![CDATA[<p>Every .NET programmers is used to brace matching, code collapsing and Intellisense because Visual Studio got them from the beginning, I expect to have these helpers when I write jscript inside a cshtml file too.</p>
<h5>Intellisense</h5>
<p>For Intellisense in .js files add this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4e20ff35-5cc6-496c-8d5f-df5278a494a6" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">index.js</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#006400">/// &lt;reference path=&quot;jquery-1.7.1-vsdoc.js&quot; /&gt;</span><br /> <span style="color:#006400">/// &lt;reference path=&quot;jquery.validate-vsdoc.js&quot; /&gt;</span></div>
</p></div>
</p></div>
<p>In order to enable JQuery Intellisense for Asp.NET MVC projects that are using Razor syntax, you’ll need to insert in every cshtml this piece of code:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8678daac-d2f2-41af-8590-e95431adc08f" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="background:#ffff00">@</span><span style="color:#0000ff">if</span> (<span style="color:#0000ff">false</span>)<br /> {<br />     <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;../../Scripts/jquery-1.7.1-vsdoc.js&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span><br /> }</div>
</p></div>
</p></div>
<p>It’s important to add the script reference inside a If(false) clause, in this way at runtime your html code will not contain the vsdoc reference. After adding the code you’ll have Intellisense with comments support:</p>
<p><img class="alignnone size-full wp-image-520" title="jquery-intellisense" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/05/jquery-intellisense.png" width="656" height="148" /></p>
<p>&#160;</p>
<p>Notice that I am using the <a href="http://www.stefanprodan.eu/2011/05/update-fix-jquery-1-6-1/" target="_blank">latest version of JQuery</a>, in order to update your MVC project please follows the steps described in this <a href="http://www.stefanprodan.eu/2011/05/updat-mvc-projects-to-jquery-1-6/">post</a>.</p>
<p><em>NuGet Package</em>: <a href="http://www.nuget.org/List/Packages/jQuery" target="_blank">JQuery</a></p>
<h5>Better JScript Editor</h5>
<p>On the Visual Studio Gallery website there is great plugin that will add outlining, brace matching, code collapsing, word highlighting and &lt;para&gt; tag support. You can install it via this <a href="http://visualstudiogallery.msdn.microsoft.com/872d27ee-38c7-4a97-98dc-0d8a431cc2ed" target="_blank">link</a> and you’ll benefit from these feature inside &lt;script&gt; areas.</p>
<p><em>Download</em>: <a href="http://visualstudiogallery.msdn.microsoft.com/872d27ee-38c7-4a97-98dc-0d8a431cc2ed" target="_blank">JScript Editor Extensions</a></p>
<p><img class="size-full wp-image-521 alignnone" title="jscript-ext" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/05/jscript-ext.png" width="180" height="180" /></p>
<p>If you want to use code regions inside .js files and have outlining like in C# editor (outlines {}s, []s and #region tags) get JSEnhancements, is the right tool and works alongside Microsoft JScript Editor Extensions.</p>
<p><em>Download</em>: <a href="http://visualstudiogallery.msdn.microsoft.com/0696ad60-1c68-4b2a-9646-4b5f4f8f2e06" target="_blank">JSEnhancements</a></p>
<p><img class="size-full wp-image-570 alignnone" title="JSEnhancements" alt="" src="http://www.stefanprodan.eu/wp-content/uploads/2011/05/JSEnhancements.png" width="229" height="395" /></p>
<p><strong></strong></p>
<p><strong>Visual Studio 11</strong> will support ECMAScript 5 and many more IntelliSense improvements regarding javascript, for more details check this blog post&#160; <a href="http://blogs.msdn.com/b/webdevtools/archive/2011/09/15/new-javascript-editing-features-for-web-development-in-visual-studio-11-developer-preview.aspx" target="_blank">New JavaScript editing features for Web development in Visual Studio 11 Developer Preview</a>.</p>
<p><strong>Last update on:</strong> 10 February 2012</p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/SJgOqGVB04Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/05/improve-visual-studio-2010-jscript-editor-with-jquery-intellisense-and-code-outlining/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/05/improve-visual-studio-2010-jscript-editor-with-jquery-intellisense-and-code-outlining/</feedburner:origLink></item>
		<item>
		<title>Send Date Time Objects with JQuery to Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/9mM88-TvS6s/</link>
		<comments>http://www.stefanprodan.eu/2011/05/send-date-time-objects-with-jquery-to-asp-net-mvc/#comments</comments>
		<pubDate>Tue, 17 May 2011 23:25:40 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=514</guid>
		<description><![CDATA[I was looking for a way to create objects on the client side with JQuery and send them by ajax to a MVC controller, I want to be able to...]]></description>
			<content:encoded><![CDATA[<p>I was looking for a way to create objects on the client side with JQuery and send them by ajax to a MVC controller, I want to be able to fill my objects with DateTime instances and then serialize them in such a way that they will be auto mapped to the corresponding C# object. In order to accomplish this I had to format the date with JavaScript in some way that whatever the time zone is on the client side, I will get on the controller the local time of the host IIS server. </p>
<h5>Sending simple date object</h5>
<p>On the controller side I’ve created a method that accepts a DateTime parameter:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:624be005-344a-432d-ab78-cd0d318f668a" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">HomeController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">        [<span style="color:#2b91af">HttpPost</span>]<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> ValbyPost(<span style="color:#2b91af">DateTime</span> inputDate)<br />         {<br />             <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span><br />             {<br />                 Data = inputDate.ToString()<br />             });<br />         }</div>
</p></div>
</p></div>
<p>On the client side I’ve created a new Date object with JQuery, then the date is formatted to ISO UTC format and sent over ajax:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:54f279d2-896a-4794-8681-1274927242c6" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">src</span><span style="color:#0000ff">=&quot;</span><span style="background:#ffff00">@</span><span style="color:#0000ff">Url.Content(</span><span style="color:#a31515">&quot;~/Scripts/date.format.js&quot;</span><span style="color:#0000ff">)&quot;</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#800000">script</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">=&quot;text/javascript&quot;&gt;</span></p>
<p>     $(<span style="color:#800000">&#39;#btn1&#39;</span>).click(<span style="color:#0000ff">function</span> () {<br />         <span style="color:#0000ff">var</span> myVal = <span style="color:#0000ff">new</span> Date();<br />         <span style="color:#006400">//format the date for the controller &#8211; c#</span><br />         myVal = dateFormat(myVal, <span style="color:#800000">&#39;isoUtcDateTime&#39;</span>);<br />         <span style="color:#006400">//send to server</span><br />         $.ajax({<br />             type: <span style="color:#800000">&quot;POST&quot;</span>,<br />             url: <span style="color:#800000">&quot;Home/ValbyPost&quot;</span>,<br />             dataType: <span style="color:#800000">&quot;JSON&quot;</span>,<br />             data: { inputDate: myVal },<br />             success: <span style="color:#0000ff">function</span> (data) {<br />                 alert(<span style="color:#800000">&#39;Http POST &#8211; &#39;</span> + data.Data);<br />             }<br />         });<br />     });</p>
<p> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">script</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>Since my time zone +2GMT I expect to get the right result in C#, when inspecting the http POST with Firebug this is the outcome:</p>
<p><em>Sent data:</em>&#160; 2011-05-17T22:43:53Z (client side <strong>UTC</strong>)</p>
<p><em>Received data:</em> 5/18/2011 1:43:53 AM (server side <strong>+2GMT</strong>)</p>
<p>Notice that I am using the dateFormat method from the date.format.js, this helper is created by Steven Levithan back in 2007 and is a ColdFusion-inspired script, you can read more about it on this <a href="http://blog.stevenlevithan.com/archives/date-format-1-0" target="_blank">post</a>. </p>
<h5>Sending complex date object</h5>
<p>Another frequent case is when you have a class defined in C# that contains DateTime members and you want to create and send with JQuery this object to the server. I’ve created a demo class named History that has several properties like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e404827f-8e37-46f8-be4d-2f725705554a" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">DateTimeMvc.Models.History.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">History</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTime</span> LastModified { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">DateTime</span>&gt; Versions { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }</div>
</p></div>
</p></div>
<p>And on the in the Home controller there is a C# method that has a parameter of type History:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0be4beab-c178-447f-b097-48b05f247017" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">HomeController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">        [<span style="color:#2b91af">HttpPost</span>]<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> ObjbyPost(<span style="color:#2b91af">History</span> history)<br />         {<br />             <span style="color:#0000ff">string</span> msg = <span style="color:#0000ff">string</span>.Empty;<br />             <span style="color:#0000ff">if</span> (history.Versions != <span style="color:#0000ff">null</span> &amp;&amp; history.Versions.Count &gt; 0)<br />             {<br />                 <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> item <span style="color:#0000ff">in</span> history.Versions)<br />                 {<br />                     msg += <span style="color:#a31515">&quot; &quot;</span> + item.ToShortDateString() + <span style="color:#a31515">&quot; &quot;</span>;<br />                 }<br />             }</p>
<p>             <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span><br />             {<br />                 Data = msg.Length &gt; 0 ? msg : <span style="color:#a31515">&quot; NO data&quot;</span><br />             });<br />         }</div>
</p></div>
</p></div>
<p>And this is how you can create and format the History object in JQuery in order to send the object by ajax to the server:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e7cc2d69-eb18-4815-952c-f6c781f03d3b" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    $(<span style="color:#800000">&#39;#btn2&#39;</span>).click(<span style="color:#0000ff">function</span> () {<br />         <span style="color:#006400">//make some data</span><br />         <span style="color:#0000ff">var</span> myVal = <span style="color:#0000ff">new</span> Date();<br />         myVal = dateFormat(myVal, <span style="color:#800000">&#39;isoUtcDateTime&#39;</span>);</p>
<p>         <span style="color:#0000ff">var</span> dt1 = <span style="color:#0000ff">new</span> Date().setYear(2010);<br />         <span style="color:#0000ff">var</span> dt2 = <span style="color:#0000ff">new</span> Date().setYear(2009);<br />         dt1 = dateFormat(dt1, <span style="color:#800000">&#39;isoUtcDateTime&#39;</span>);<br />         dt2 = dateFormat(dt2, <span style="color:#800000">&#39;isoUtcDateTime&#39;</span>);</p>
<p>         <span style="color:#006400">//create a new history object in js same as in c#</span><br />         <span style="color:#0000ff">var</span> history = {<br />             LastModified: myVal,<br />             Versions: <span style="color:#0000ff">new</span> Array()<br />         }</p>
<p>         <span style="color:#006400">//create history entries</span><br />         history.Versions.push(dt1);<br />         history.Versions.push(dt2);</p>
<p>         <span style="color:#006400">//send to server using http post</span><br />         $.ajax({<br />             type: <span style="color:#800000">&quot;POST&quot;</span>,<br />             url: <span style="color:#800000">&quot;Home/ObjbyPost&quot;</span>,<br />             contentType: <span style="color:#800000">&quot;application/json; charset=utf-8&quot;</span>,<br />             dataType: <span style="color:#800000">&quot;JSON&quot;</span>,<br />             data: JSON.stringify(history),<br />             success: <span style="color:#0000ff">function</span> (data) {<br />                 alert(<span style="color:#800000">&#39;Http POST &#8211; &#39;</span> + data.Data);<br />             }<br />         });<br />     });</div>
</p></div>
</p></div>
<p>After creating the date object with JQuery the iso format is applied, then the History object is <em>stringify</em> with JSON and sent to the controller, as in the first example the History class instance is received in C# with the right time zone. But if you’ll change from POST to GET this method will fail and the controller will receive null instead of dates. After debugging with Firebug I’ve come to the conclusion that the MVC will not auto deserialize the json object so I’ve changed the method to receive a string and did a manual deserialization with JavaScriptSerializer.&#160; </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1f6b544b-7e9a-4755-a4ea-3d9831f9c960" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">HomeController.cs</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">        [<span style="color:#2b91af">HttpGet</span>]<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">JsonResult</span> ObjbyGet(<span style="color:#0000ff">string</span> history)<br />         {<br />             <span style="color:#0000ff">var</span> serializer = <span style="color:#0000ff">new</span> <span style="color:#2b91af">JavaScriptSerializer</span>();<br />             <span style="color:#0000ff">var</span> obj = serializer.Deserialize&lt;<span style="color:#2b91af">History</span>&gt;(history);</p>
<p>             <span style="color:#0000ff">string</span> msg = <span style="color:#0000ff">string</span>.Empty;<br />             <span style="color:#0000ff">if</span> (obj.Versions != <span style="color:#0000ff">null</span> &amp;&amp; obj.Versions.Count &gt; 0)<br />             {<br />                 <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> item <span style="color:#0000ff">in</span> obj.Versions)<br />                 {<br />                     msg += <span style="color:#a31515">&quot; &quot;</span> + item.ToShortDateString() + <span style="color:#a31515">&quot; &quot;</span>;<br />                 }<br />             }</p>
<p>             <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span><br />             {<br />                 Data = msg.Length &gt; 0 ? msg : <span style="color:#a31515">&quot; NO data&quot;</span><br />             }, <span style="color:#2b91af">JsonRequestBehavior</span>.AllowGet);<br />         }</div>
</p></div>
</p></div>
<p>Changes on the client side code:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:63d2f597-9c26-4000-beb1-8d2a3bc43c57" class="wlWriterEditableSmartContent">
<div class="le-pavsc-container">
<div class="le-pavsc-titleblock">Index.cshtml</div>
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">        <span style="color:#006400">//send to server using http get</span><br />         $.ajax({<br />             type: <span style="color:#800000">&quot;GET&quot;</span>,<br />             url: <span style="color:#800000">&quot;Home/ObjbyGet&quot;</span>,<br />             dataType: <span style="color:#800000">&quot;JSON&quot;</span>,<br />             data: { history: JSON.stringify(history) },<br />             success: <span style="color:#0000ff">function</span> (data) {<br />                 alert(<span style="color:#800000">&#39;Http GET &#8211; &#39;</span> + data.Data);<br />             }<br />         });</div>
</p></div>
</p></div>
<p>Be careful when using Http GET, it’s not suitable for large objects because the size is limited and by default is ASCI encoded, the Http POST is slower but as you see in the above code you can send object directly without needing the JavaScriptSerializer.</p>
<p><a href="http://www.stefanprodan.eu/admin/code/DateTimeMvc.zip"><img class="size-full wp-image-104" title="download source code" alt="download source code files" src="http://www.stefanprodan.eu/wp-content/uploads/2011/02/downloadsc.png" width="186" height="22" /></a></p>
<p>Beside this code, the demo project contains a search form with a start date and a end date that are serialized with JQuery, check it out. The project is made with Asp.Net MVC 3 and JQuery 1.6.1, in order to run it you’ll need VS.NET 2010 and the MVC Tools Update installed.</p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/9mM88-TvS6s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/05/send-date-time-objects-with-jquery-to-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/05/send-date-time-objects-with-jquery-to-asp-net-mvc/</feedburner:origLink></item>
		<item>
		<title>Update fix JQuery 1.6.1</title>
		<link>http://feedproxy.google.com/~r/StefanTechNotes/~3/MkL1pj1QMpQ/</link>
		<comments>http://www.stefanprodan.eu/2011/05/update-fix-jquery-1-6-1/#comments</comments>
		<pubDate>Sun, 15 May 2011 20:39:30 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[Archive]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.stefanprodan.eu/?p=501</guid>
		<description><![CDATA[As you may well know, at the beginning of this month, JQuery 1.6 was released, in version 1.6 the Attribute module was rewritten and a new method named .prop() emerged....]]></description>
			<content:encoded><![CDATA[<p>As you may well know, at the beginning of this month, JQuery 1.6 was released, in version 1.6 the Attribute module was rewritten and a new method named .prop() emerged. Because .prop() took over the boolean attributes/properties, code previously made for version 1.5.1 stopped working.</p>
<p>The 1.6.1 release does the wright thing and made possible for the old code to work without any changes, what it does is deferring the call from .attr() to .prop() when needed. Thank you JQuery Team!</p>
<h5>.attr() vs .prop()</h5>
<p>Because .att() still works the same way as in 1.5.1 that doesn’t mean that you have to use it wrong, as of JQuery 1.6 you should use .attr() or .prop() in these cases:</p>
<table width="200">
<tbody>
<tr>
<th width="125"><code>.attr()</code></th>
<th width="73"><code>.prop()</code></th>
</tr>
<tr>
<td width="125">accesskey</td>
<td width="73">async</td>
</tr>
<tr>
<td width="125">align</td>
<td width="73">autofocus</td>
</tr>
<tr>
<td width="125">class</td>
<td width="73">checked</td>
</tr>
<tr>
<td width="125">contenteditable</td>
<td width="73">defaultValue</td>
</tr>
<tr>
<td width="125">draggable</td>
<td width="73">disabled</td>
</tr>
<tr>
<td width="125">href</td>
<td width="73">location</td>
</tr>
<tr>
<td width="125">id</td>
<td width="73">multiple</td>
</tr>
<tr>
<td width="125">label</td>
<td width="73">nodeName</td>
</tr>
<tr>
<td width="125">rel</td>
<td width="73">nodeType</td>
</tr>
<tr>
<td width="125">src</td>
<td width="73">readOnly</td>
</tr>
<tr>
<td width="125">style</td>
<td width="73">selected</td>
</tr>
<tr>
<td width="125">tabindex</td>
<td width="73">selectedIndex</td>
</tr>
<tr>
<td width="125">title</td>
<td width="73">tagName</td>
</tr>
<tr>
<td width="125">type</td>
<td width="73"></td>
</tr>
<tr>
<td width="125">width</td>
<td width="73"></td>
</tr>
</tbody>
</table>
<h5>Reason to update</h5>
<p>What got me convinced that I need to update all my projects to version 1.6 was this graph posted on JQuery <a href="http://blog.jquery.com/" target="_blank">blog</a>, it shows the improved performance of event triggering with .data() method.</p>
<p><img src="http://farm6.static.flickr.com/5144/5683836956_3074f66856.jpg" alt="jQuery 1.6 .data() (get and set) graph" /></p>
<p>A full list of performance improvements and bug fixes can be found in this <a href="http://blog.jquery.com/2011/05/03/jquery-16-released/" target="_blank">post</a>.</p>
<h5>Update to 1.6.1 with NuGet</h5>
<p>In order to update your MVC 3 projects to the latest <a href="http://www.nuget.org/List/Packages/jQuery" target="_blank">JQuery version</a>, in Visual Studio Solution Explorer right click your project and select from the menu “Add Library Package Reference…”, on the pop-up select “Updates” and click on the package. Or from VS.NET NuGet command line run <em>PM&gt; Install-Package jQuery. </em></p>
<img src="http://feeds.feedburner.com/~r/StefanTechNotes/~4/MkL1pj1QMpQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.stefanprodan.eu/2011/05/update-fix-jquery-1-6-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.stefanprodan.eu/2011/05/update-fix-jquery-1-6-1/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic

Served from: www.stefanprodan.eu @ 2012-02-21 06:31:38 -->

