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

<channel>
	<title>Joseph Bulger IV &#187; asp.net mvc</title>
	<atom:link href="http://josephbulger.com/tag/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://josephbulger.com</link>
	<description>God, Family, Church, Engineering</description>
	<lastBuildDate>Thu, 20 Oct 2011 12:00:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET MVC Model Binding: The Ins and Outs</title>
		<link>http://josephbulger.com/programming/asp-net-mvc-model-binding-the-ins-and-outs/</link>
		<comments>http://josephbulger.com/programming/asp-net-mvc-model-binding-the-ins-and-outs/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 16:00:18 +0000</pubDate>
		<dc:creator>Joseph</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[model-binding]]></category>

		<guid isPermaLink="false">http://josephbulger.com/?p=629</guid>
		<description><![CDATA[Model Binding can be a tricky thing to get right with MVC. Dealing with flat POCOs works fine, but when you start getting into more complex, truly object oriented domain objects, things get out of hand pretty quickly. Let&#8217;s say you have a package class like this: The Package is basically flat at this point. [...]]]></description>
			<content:encoded><![CDATA[<p>Model Binding can be a tricky thing to get right with MVC. Dealing with flat POCOs works fine, but when you start getting into more complex, truly object oriented domain objects, things get out of hand pretty quickly.</p>
<p><span id="more-629"></span></p>
<p>Let&#8217;s say you have a package class like this:</p>
<div id="gist-1154718" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">namespace</span> <span class="nn">ModelBinding.Models</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">class</span> <span class="nc">Package</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="kt">string</span> <span class="n">Street</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="kt">string</span> <span class="n">City</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="kt">string</span> <span class="n">State</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="kt">int</span> <span class="n">Zip</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC10'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1154718/6ca939df75a6dc732c475adc9f2bee3d50431a9c/Model.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1154718#file_model.cs" style="float:right;margin-right:10px;color:#666">Model.cs</a>
            <a href="https://gist.github.com/1154718">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The Package is basically flat at this point. Wiring this up with Model Binding in MVC is trivial. Just create your controller to handle the class when an action get&#8217;s posted like:</p>
<div id="gist-1154718" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">using</span> <span class="nn">System</span><span class="p">;</span></div><div class='line' id='LC2'><span class="k">using</span> <span class="nn">System.Collections.Generic</span><span class="p">;</span></div><div class='line' id='LC3'><span class="k">using</span> <span class="nn">System.Linq</span><span class="p">;</span></div><div class='line' id='LC4'><span class="k">using</span> <span class="nn">System.Web</span><span class="p">;</span></div><div class='line' id='LC5'><span class="k">using</span> <span class="nn">System.Web.Mvc</span><span class="p">;</span></div><div class='line' id='LC6'><span class="k">using</span> <span class="nn">ModelBinding.Models</span><span class="p">;</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="k">namespace</span> <span class="nn">ModelBinding.Controllers</span></div><div class='line' id='LC9'><span class="p">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">class</span> <span class="nc">HomeController</span> <span class="p">:</span> <span class="n">Controller</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">ActionResult</span> <span class="nf">Index</span><span class="p">()</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ViewBag</span><span class="p">.</span><span class="n">Message</span> <span class="p">=</span> <span class="s">&quot;Welcome to ASP.NET MVC!&quot;</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nf">View</span><span class="p">(</span><span class="k">new</span> <span class="n">Package</span><span class="p">());</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'><span class="na">        [HttpPost]</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">ActionResult</span> <span class="nf">Index</span><span class="p">(</span><span class="n">Package</span> <span class="n">model</span><span class="p">)</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nf">Json</span><span class="p">(</span><span class="n">model</span><span class="p">);</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC25'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1154718/1471de0070acb9c1af14ca8669538d88d2a75550/Controller.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1154718#file_controller.cs" style="float:right;margin-right:10px;color:#666">Controller.cs</a>
            <a href="https://gist.github.com/1154718">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>and make a view that uses the class as it&#8217;s model:</p>
<div id="gist-1154718" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>@model ModelBinding.Models.Package</div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC3'>@{</div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;ViewBag.Title = &quot;Home Page&quot;;</div><div class='line' id='LC5'>}</div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&lt;h2&gt;@ViewBag.Message&lt;/h2&gt;</div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>@using (Html.BeginForm(&quot;Index&quot;, &quot;Home&quot;))</div><div class='line' id='LC10'>{</div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;@Html.EditorForModel()</div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;lets do this&quot; /&gt;</div><div class='line' id='LC14'>}</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1154718/9a1ff9aceaecba123e479312083f4136351b2a22/View" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1154718#file_view" style="float:right;margin-right:10px;color:#666">View</a>
            <a href="https://gist.github.com/1154718">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This code works fine. The problem is I don&#8217;t like the code! My package class is all wrong. All those properties should be inside an Address Class, which is <strong>used by </strong>the Package.</p>
<p>Once you make a more complicated model than a flat one, everything starts breaking. The problem is that so many things break you have to go through and fix all the pieces one at a time, so we&#8217;ll have to go through each of these issues in a post by themselves.</p>
<p>The first thing we&#8217;re going to address is this: when you change the Package to have an Address, the View shows <strong>nothing</strong> on a GET. What happened to the View? We&#8217;ll cover that next.</p>
]]></content:encoded>
			<wfw:commentRss>http://josephbulger.com/programming/asp-net-mvc-model-binding-the-ins-and-outs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating an PagedList&lt;T&gt; that uses AJAX</title>
		<link>http://josephbulger.com/technology/creating-an-pagedlistt-that-uses-ajax/</link>
		<comments>http://josephbulger.com/technology/creating-an-pagedlistt-that-uses-ajax/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 16:05:58 +0000</pubDate>
		<dc:creator>Joseph</dc:creator>
				<category><![CDATA[Consulting]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Showcase]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[linkedIn]]></category>
		<category><![CDATA[paging]]></category>

		<guid isPermaLink="false">http://josephbulger.com/technology/creating-an-pagedlistt-that-uses-ajax/</guid>
		<description><![CDATA[I’ve been using this PagedList functionality that i found from a blog article Rob Conery put up, and a control I found by Robert Muehsig which I’ve really enjoyed using so far. One of the things that was missing from the functional set that I ended up needing was the ability to page the list, [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been using this PagedList functionality that i found from a <a href="http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/" target="_blank">blog article Rob Conery</a> put up, and <a href="http://code-inside.de/blog-in/2008/04/08/aspnet-mvc-pagination-view-user-control/" target="_blank">a control I found by Robert Muehsig</a> which I’ve really enjoyed using so far.</p>
<p>One of the things that was missing from the functional set that I ended up needing was the ability to page the list, but through issuing AJAX requests instead of the typical post back.</p>
<p>So I went off and extended the existing model to support AJAX requests, and thought I would share it in case anyone else needed to do the same thing.<span id="more-249"></span></p>
<p>I guess the best place to start would be the use case.  So to start I created a control that encapsulates the Paging UI layout and calls I need.  The use of the original control looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">

Html.RenderPartial(&quot;AjaxPagination&quot;,
    new AjaxPaginationViewData
        {
            PageIndex = Model.PageIndex,
            Action = &quot;CondoPage&quot;,
            Controller = &quot;Home&quot;,
            AjaxOptions =
                new AjaxOptions { UpdateTargetId = &quot;updatedContent&quot; },
            TotalCount = Model.TotalCount,
            PageSize = Model.PageSize,
            NumberOfPagesToEachSide = 2
        }
);
</pre>
<p>The new AJAX functionality is called similarly:</p>
<pre class="brush: csharp; title: ; notranslate">

&lt;% using (Ajax.BeginForm(&quot;SomePage&quot;,
        &quot;SomeController&quot;,
        new AjaxOptions { UpdateTargetId = &quot;updatedContent&quot; })) { %&gt;

        &lt;% Html.RenderPartial(&quot;AjaxPagination&quot;,
                new AjaxPaginationViewData {
                        PageIndex = Model.PageIndex,
                        Action = &quot;SomeAction&quot;,
                        Controller = &quot;SomeController&quot;,
                        AjaxOptions = new AjaxOptions
                                { UpdateTargetId = &quot;updatedContent&quot; },
                        TotalCount = Model.TotalCount,
                        PageSize = Model.PageSize,
                        NumberOfPagesToEachSide = 2
                });%&gt;

&lt;% } %&gt;
</pre>
<p>A couple things to note. You’ll notice that the AJAX control is rendered inside a Ajax.BeginForm. This is because I’m using the Microsoft.Ajax way of making AJAX calls.  This could also be done using jQuery or something else that can process AJAX calls. I just went this way because the scripts are already included in asp.net mvc app when you first create the project.  The result of the AJAX call will be a partial view, and we’ll need to put that somewhere.  That’s where the UpdatedTargetId comes into play. Other things we include in the AJAX control that are not in the original are the Action and the Controller, and some AjaxOptions. PageActionLink doesn’t work with the AJAX control, because we’ll be using Ajax.ActionLink to build the link, which is why I broke it up into Action, and Controller. For the AjaxOptions, we need those to specify the target of the call.</p>
<p>So now that’s been explained, let’s look at the controls themselves.  Here’s a comparison of the original control and the ajax control.</p>
<p>The original is one this way:</p>
<pre class="brush: csharp; title: ; notranslate">

&lt;% if (Model.HasPreviousPage) { %&gt;
    &lt;a href=&quot;&lt;%=Model.PageActionLink.Replace(&quot;%7Bpage%7D&quot;, (Model.PageIndex - 1).ToString())%&gt;&quot;&gt;Previous&lt;/a&gt;
&lt;% } %&gt;

&lt;% if (Model.GetFirstPageToLink() != 1) { %&gt;...&lt;% } %&gt;

&lt;%for (var page = Model.GetFirstPageToLink(); page &lt;= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %&gt;
        &lt;%=page.ToString()%&gt;
&lt;% } else { %&gt;
    &lt;a href=&quot;&lt;%=Model.PageActionLink.Replace(&quot;%7Bpage%7D&quot;, page.ToString())%&gt;&quot;&gt;&lt;%=page.ToString()%&gt;&lt;/a&gt;
&lt;% }

    if (page != Model.GetLastPageToLink()) { %&gt;|&lt;% } } %&gt;

&lt;% if (Model.GetLastPageToLink() != Model.PageCount) { %&gt;...&lt;% } %&gt;

&lt;% if (Model.HasNextPage) { %&gt;
    &lt;a href=&quot;&lt;%=Model.PageActionLink.Replace(&quot;%7Bpage%7D&quot;, (Model.PageIndex + 1).ToString())%&gt;&quot;&gt;Next&lt;/a&gt;
&lt;% } %&gt;
</pre>
<p>And the AJAX control is done this way:</p>
<pre class="brush: csharp; title: ; notranslate">

&lt;% if (Model.HasPreviousPage) { %&gt;

&lt;%= Ajax.ActionLink(&quot;Previous&quot;, Model.Action, Model.Controller, new { page = (Model.PageIndex - 1).ToString() }, Model.AjaxOptions)%&gt;

&lt;% } %&gt;

&lt;% if (Model.GetFirstPageToLink() != 1) { %&gt;...&lt;% } %&gt;

&lt;%for (var page = Model.GetFirstPageToLink(); page &lt;= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %&gt;
        &lt;%=page.ToString()%&gt;
    &lt;% } else { %&gt;

&lt;%= Ajax.ActionLink(page.ToString(), Model.Action, Model.Controller, new { page = page.ToString() }, Model.AjaxOptions)%&gt;

&lt;% } if (page != Model.GetLastPageToLink()) { %&gt; | &lt;% } } %&gt;

&lt;% if (Model.GetLastPageToLink() != Model.PageCount) { %&gt;...&lt;% } %&gt;

&lt;% if (Model.HasNextPage) { %&gt;

&lt;%= Ajax.ActionLink(&quot;Next&quot;, Model.Action, Model.Controller, new { page = (Model.PageIndex + 1).ToString() }, Model.AjaxOptions)%&gt;

&lt;% } %&gt;
</pre>
<p>The big difference here is the way that the links are generated. The original control simply creates an anchor tag and passes in the url generated by the Model. The AJAX control uses AJAX.ActionLink() instead, so we can have the link support AJAX.</p>
<p>So knowing how the control looks, this is the Model for the AJAX control itself:</p>
<pre class="brush: csharp; title: ; notranslate">

public class AjaxPaginationViewData
{
    public int NumberOfPagesToEachSide { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }

    public string Action { get; set; }
    public string Controller { get; set; }

    public AjaxOptions AjaxOptions { get; set; }

    public int PageCount
    {
        get
        {
            return (int)Math.Ceiling((double)TotalCount / PageSize);
        }
    }
    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex &gt; 1);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex * PageSize) &lt;= TotalCount;
        }
    }

    public int GetFirstPageToLink()
    {
        return (PageIndex - NumberOfPagesToEachSide &gt; 1 ? PageIndex - NumberOfPagesToEachSide : 1);
    }

    public int GetLastPageToLink()
    {
        return (PageIndex + NumberOfPagesToEachSide &lt; PageCount ? PageIndex + NumberOfPagesToEachSide : PageCount);
    }
}
</pre>
<p>That pretty much explains how the control is built.</p>
<p>The only thing left is how the interaction with PagedList happens.  For that we look at the action that the control calls.  In this example, we’re calling SomeAction in SomeController, and it would look something like this:</p>
<pre class="brush: csharp; title: ; notranslate">

public ActionResult SomeAction(int page)
{
    CachedPage = page;
    var query = GetSearchQuery(CachedSearchParameters);
    var model = query.ToPagedList(page, DefaultPageSize);
    return PartialView(&quot;AjaxResults&quot;, model);
}
</pre>
<p>The ToPagedList performs the functionality that is included with the PagedList classes which you can find <a href="http://pagedlist.codeplex.com/" target="_blank">here</a>.</p>
<p>Let me know what you think, and if you’d like some demo source to see this in action I can happily provide, just let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://josephbulger.com/technology/creating-an-pagedlistt-that-uses-ajax/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

