<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'MVC' and 'Web'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=MVC,Web&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'MVC' and 'Web'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Unit Testing ASP.NET MVC 2.0 With TypeMock</title><link>http://msmvps.com/blogs/bmains/archive/2010/05/25/unit-testing-asp-net-mvc-2-0-with-typemock.aspx</link><pubDate>Tue, 25 May 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1769972</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;One of the selling points of ASP.NET MVC 2.0 was the ability to unit test your action methods.&amp;nbsp; Even though Microsoft made MVC flexible by making it easier to unit test than web forms, it isn&amp;#39;t always easy to implement without writing a lot of code to implement fakes for common services you use or without abstracting the interface altogether.&amp;nbsp; For instance, the HttpContextBase class is an abstract base class that you can use to abstract using the context, but you&amp;#39;d have to create a class that inherits from this and implement the respective methods.&amp;nbsp; As another example, you&amp;#39;ve probably seen controllers that implement an interface like IProductsRepository, and use an injection tool like Castle Windsor or Microsoft Unity to inject a reference into the controller&amp;#39;s constructor.&lt;/p&gt;
&lt;p&gt;Enter mocking libraries, making this process easier to do.&amp;nbsp; TypeMock makes this very easy to do, and you don&amp;#39;t even need to write a fake or worry about dynamic injection (unless you want to, because after all, DI is pretty cool and flexible).&amp;nbsp; Let&amp;#39;s look at an example controller action method below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class ProductsController : Controller&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; public ActionResult Get(int key)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var repos = new ProductsRepository();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var product = repos.GetByKey(key);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if (product == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; throw new Exception(&amp;quot;The product couldn&amp;#39;t be found&amp;quot;);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return View(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;Here we have a reference to the ProductsRepository; it&amp;#39;s a repository class serving up data from a LINQ to SQL backend.&amp;nbsp; TypeMock makes it easy to construct some tests to ensure that the action method works as expected, that it throws the error when the product is null, and that it returns the view when the product exists.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;First Test - Forcing an Exception&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The first test verifies that when no product returns from the database, an exception is thrown.&lt;/p&gt;
&lt;p&gt;[Test]&lt;br /&gt;public void Get_ReturningNullProductFromDBThrowsException()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Arrange&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var repos = Isolate.Fake.Instance&amp;lt;ProductsRepository&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.WhenCalled(() =&amp;gt; repos.GetByKey(123)).WillReturn(null);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.Swap.NextInstance&amp;lt;ProductsRepository&amp;gt;().With(repos);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var controller = Isolate.Fake.Instance&amp;lt;ProductsController&amp;gt;(Members.CallOriginal);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Act&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var result = controller.Get(123);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Fail();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.AreEqual(&amp;quot;The product couldn&amp;#39;t be found&amp;quot;, ex.Message);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.CleanUp();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;TypeMock&amp;#39;s Isolate.Fake mechanism creates a fake for the product repository, and overtakes the GetByKey method to force a null returned value.&amp;nbsp; This works great to force the result, but we need the SwapNextInstance call to ensure that the next new ProductsRepository statement returns the fake.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Second Test - Finding Product OK&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The second test checks that a valid product gets served up to the view.&amp;nbsp; The way to check that is to invoke the action result of the action method, check that the result is a view result, and checks its ViewData.Model property, as illustrated below.&lt;/p&gt;
&lt;p&gt;[Test]&lt;br /&gt;public void Get_ReturningProductFromDBPassesToModel()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Arrange&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var product = new DA.Product { ProductID = 123 };&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var repos = Isolate.Fake.Instance&amp;lt;ProductsRepository&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.WhenCalled(() =&amp;gt; repos.GetByKey(123)).WillReturn(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.Swap.NextInstance&amp;lt;ProductsRepository&amp;gt;().With(repos);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var controller = Isolate.Fake.Instance&amp;lt;ProductsController&amp;gt;(Members.CallOriginal);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Act&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var result = controller.Get(123);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Assert&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.IsInstanceOf&amp;lt;ViewResult&amp;gt;(result);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.AreEqual(product, ((ViewResult)result).ViewData.Model);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.CleanUp();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Instead of returning a null product, this test returns a dummy Product instance through a fake ProductsRepository.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Third Test - Exception Bubbling&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The third check tests that when an exception occurs within the product repository, that the exception bubbles up from the repository.&lt;/p&gt;
&lt;p&gt;[Test]&lt;br /&gt;public void Get_ErrorFromProviderBubblesUp()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Arrange&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var repos = Isolate.Fake.Instance&amp;lt;ProductsRepository&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.WhenCalled(() =&amp;gt; repos.GetByKey(123)).WillThrow(new Exception(&amp;quot;Test&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.Swap.NextInstance&amp;lt;ProductsRepository&amp;gt;().With(repos);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var controller = Isolate.Fake.Instance&amp;lt;ProductsController&amp;gt;(Members.CallOriginal);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Act&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var result = controller.Get(123);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Fail();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Assert&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.AreEqual(&amp;quot;Test&amp;quot;, ex.Message);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Isolate.CleanUp();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Instead of returning a product, the WillThrow method simulates throwing an exception.&amp;nbsp; When calling the Get method, should throw an exception, which it does pass the test.&lt;/p&gt;</description></item><item><title>Rendering Inline using HTML.Action in MVC 2.0</title><link>http://msmvps.com/blogs/bmains/archive/2010/04/25/rendering-inline-using-html-action-in-mvc-2-0.aspx</link><pubDate>Sun, 25 Apr 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764248</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;Html.Action is a method method that injects a response from the server directly into the UI.&amp;nbsp; For instance, a controller can have two action methods, then inject the response directly into the UI from the other controller.&amp;nbsp; Take a look at the sample form below:&lt;/p&gt;
&lt;p&gt;&amp;lt;p&amp;gt;&lt;br /&gt;&amp;nbsp;Main Action&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp; Name: &amp;lt;%= Html.TextBox(&amp;quot;IndexName&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp; Value: &amp;lt;%= Html.TextBox(&amp;quot;IndexValue&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;save&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.Action(&amp;quot;Custom&amp;quot;, new { title = &amp;quot;Test Title&amp;quot; }) %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The custom action gets invoked and injected into the main view.&amp;nbsp; Any parameters that get passed to the action method get passed as a routing parameter, as shown here (a title property).&amp;nbsp; Below is the sample controller methods that return the view to inject.&lt;/p&gt;
&lt;p&gt;[HttpGet, ChildActionOnly]&lt;br /&gt;public ActionResult Custom()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;return PartialView();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;[HttpPost]&lt;br /&gt;public ActionResult Custom(FormCollection form)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;return PartialView();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;[HttpGet]&lt;br /&gt;public ActionResult Index()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;return View();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;The main view calls the Index method to get the response, that view calls the Custom action method.&amp;nbsp; Note the ChildActionOnly attribute; this designates that the action method can only be used within a main view.&amp;nbsp; The Custom action method returns a PartialView to insert a partial response into the UI, rather than including a whole view.&amp;nbsp; The child view below is what gets injected.&amp;nbsp; This partial view has its own form to receive a post to the server.&lt;/p&gt;
&lt;p&gt;&amp;lt;p&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Custom Action&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.BeginForm(&amp;quot;Custom&amp;quot;, &amp;quot;RenderingActions&amp;quot;); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name: &amp;lt;%= Html.TextBox(&amp;quot;CustomName&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value: &amp;lt;%= Html.TextBox(&amp;quot;CustomValue&amp;quot;)%&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;save&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;And that&amp;#39;s it, we can have multiple actions linked to one view, and they all can post back to their own action methods and such.&amp;nbsp; I plan to cover some of the other details at a later date.&lt;/p&gt;</description></item><item><title>Intro to Telerik MVC's DatePicker Control</title><link>http://msmvps.com/blogs/bmains/archive/2010/04/10/intro-to-telerik-mvc-s-datepicker-control.aspx</link><pubDate>Sat, 10 Apr 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1763225</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;The freely available ASP.NET MVC framework by Telerik has added a new DatePicker control that works similarly to the RadDatePicker control in its ASP.NET AJAX framework.&amp;nbsp; Below is a sample of that control:&lt;/p&gt;
&lt;p&gt;&amp;lt;%&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Html.Telerik().DatePicker().Name(&amp;quot;Picker&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .ButtonTitle(&amp;quot;Select Date&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .ClientEvents((evt) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; evt.OnChange(() =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; function(e) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $(&amp;quot;#output&amp;quot;).html(&amp;quot;Value changed to &amp;quot; + e.date);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;%&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; })&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Value(DateTime.Now)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .MaxDate(DateTime.Now.AddYears(3))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .MinDate(DateTime.Now.Subtract(new TimeSpan(1000, 0, 0, 0)))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Render();&lt;br /&gt;%&amp;gt;&lt;/p&gt;
&lt;p&gt;Note that this date picker has some of the same properties as the calendar in my previous post.&amp;nbsp; The MaxDate, MinDate, ClientEvents, etc are the same properties available in both, as you have access to a wide array of options available to you).&amp;nbsp; The value property allows the defaulting of the current date as I&amp;#39;ve currently set, as well as a tooltip for the button, which is shown as a tooltip in the image below.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/6303.datepicker1.png"&gt;&lt;img src="http://msmvps.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/6303.datepicker1.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Clicking on the button shows a calendar control.&amp;nbsp; Clicking on the date fires our client-side change handler.&amp;nbsp; Our handler writes out the selected date to the browser as is, which renders the option below.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/1680.datepicker2.png"&gt;&lt;img src="http://msmvps.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/1680.datepicker2.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Currently, the selected date is 4/13/2010; clicking on the 22nd will fire the change event, making available an e.date property with the new date.&amp;nbsp; By using the client-side equivalent handler, for both the date picker and calendar, you avoid the postback to the server to an action method, saving on resources.&lt;/p&gt;</description></item><item><title>Telerik Calendar Control Intro for ASP.NET MVC</title><link>http://msmvps.com/blogs/bmains/archive/2010/04/08/telerik-calendar-control-intro-for-asp-net-mvc.aspx</link><pubDate>Thu, 08 Apr 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1763122</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;The latest release of the open source Telerik ASP.NET MVC framework contains a new calendar control.&amp;nbsp; This control functions similar to the ASP.NET AJAX calendar extender calendar, where it shows a month view, year view, and decade view, and responds to clicks on the client.&amp;nbsp; Telerik can also specially process clicks for the server too.&amp;nbsp; Let&amp;#39;s take a look at the calendar.&amp;nbsp; I&amp;#39;m assuming you are familiar with the telerik component setup.&lt;/p&gt;
&lt;p&gt;&amp;lt;% &lt;br /&gt;Html.Telerik().Calendar().Name(&amp;quot;Cal1&amp;quot;)&lt;br /&gt;&amp;nbsp;.MinDate(new DateTime(DateTime.Today.Year - 1, 1, 1))&lt;br /&gt;&amp;nbsp;.MaxDate(new DateTime(DateTime.Today.Year + 1, 12, 31))&lt;br /&gt;&amp;nbsp;.Selection((sel) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sel.Action(&amp;quot;SelectedDate&amp;quot;, new { selected = true });&lt;br /&gt;&amp;nbsp;&amp;nbsp;}).Render();&lt;br /&gt;%&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The calendar control can regulate the minimum and maximum dates that can be selected (here a three year range is valid).&amp;nbsp; This restriction is valid for all three calendar views too.&amp;nbsp; The other option processes a calendar click by redirecting to another action method (hence the Action method) called SelectedDate.&lt;/p&gt;
&lt;p&gt;The calendar (using the Vista style) looks like the following in month view:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/1565.calendar1.png"&gt;&lt;img src="http://msmvps.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/1565.calendar1.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Clicking on the header goes into year view:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/2158.calendar2.png"&gt;&lt;img src="http://msmvps.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/2158.calendar2.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Clicking on the header again goes into decade view:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/7848.calendar3.png"&gt;&lt;img src="http://msmvps.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/bmains/7848.calendar3.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And drilling back down by selecting the year, month, and day causes a redirection to the URL &lt;a href="http://localhost/Calendar/SelectedDate?selected=4/8/2010"&gt;http://localhost/Calendar/SelectedDate?selected=4/8/2010&lt;/a&gt;, which is an action method in the calendar controller that we specified above.&amp;nbsp; Also, we specified a selected property with a boolean; that boolean gets overridden and it uses the selected property to store the selected date.&amp;nbsp; You can call this whatever you want.&lt;/p&gt;</description></item><item><title>Generated ID's from Model Binding</title><link>http://msmvps.com/blogs/bmains/archive/2010/04/02/generated-id-s-from-model-binding.aspx</link><pubDate>Fri, 02 Apr 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1762817</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;With the new various For methods in MVC (DisplayFor, TextBoxFor, etc.), these methods use a lambda to specify the fields to display/render.&amp;nbsp; The following form below:&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;lt;%= Html.ValidationSummary(&amp;quot;The following errors have occurred:&amp;quot;) %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;lt;%= Html.Hidden(&amp;quot;Story.ProjectPhaseKey&amp;quot;, this.Request.QueryString.Get(&amp;quot;phase&amp;quot;)) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;%= Html.Hidden(&amp;quot;Story.ProjectKey&amp;quot;, this.RouteData.Values[&amp;quot;id&amp;quot;])%&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;span&amp;gt;Title&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.Story.Title) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;span&amp;gt;Description&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%= Html.TextAreaFor(i =&amp;gt; i.Story.Description) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Save Changes&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Renders the following:&lt;/p&gt;
&lt;p&gt;&amp;lt;input id=&amp;quot;Story_ProjectPhaseKey&amp;quot; name=&amp;quot;Story.ProjectPhaseKey&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;2&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;input id=&amp;quot;Story_ProjectKey&amp;quot; name=&amp;quot;Story.ProjectKey&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;1&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;span&amp;gt;Title&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;input id=&amp;quot;Story_Title&amp;quot; name=&amp;quot;Story.Title&amp;quot; type=&amp;quot;text&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;span&amp;gt;Description&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;textarea cols=&amp;quot;20&amp;quot; id=&amp;quot;Story_Description&amp;quot; name=&amp;quot;Story.Description&amp;quot; rows=&amp;quot;2&amp;quot;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Save Changes&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/p&gt;
&lt;p&gt;Notice how the ID&amp;#39;s use the identifier in the lambda; the i.Story.Description lambda expression evaluates to Story_Description and Story.Description, as a means to automatically populate the data for model binding.&lt;/p&gt;</description></item><item><title>Client-Side Validation Updates in MVC 2</title><link>http://msmvps.com/blogs/bmains/archive/2010/03/29/client-side-validation-updates-in-mvc-2.aspx</link><pubDate>Mon, 29 Mar 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1762540</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;&amp;nbsp;Not only does MVC support server-side validation, client-side validation is a possibility too.&amp;nbsp; Below is a sample form that sets up validation using client-side javascript.&amp;nbsp; These client-side features are made available from the MicrosoftMvcValidation.js file.&amp;nbsp; Take a look at the sample form with validation below.&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EnableClientValidation(); %&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.ValidationSummary(&amp;quot;The following errors have happened:&amp;quot;) %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;fieldset&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;legend&amp;gt;Form Values&amp;lt;/legend&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp; First: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.FirstName) %&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.FirstName, &amp;quot;*&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp; Last: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.LastName) %&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.LastName, &amp;quot;*&amp;quot;)%&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp; Email: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.Email) %&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.Email, &amp;quot;*&amp;quot;)%&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Save&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/fieldset&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;Client-side validation begins with a method call&amp;nbsp;to enable client-side validation.&amp;nbsp; Under the scenes, this enables some of the built-in features, plus ensures some of the requirements within the UI happen (for instance, it ensures that each form has a unique ID).&amp;nbsp; If you compare this example with my server-side validation blog entry, you will see the form is the same; all that&amp;#39;s different is the call to this new client-side method.&amp;nbsp; Adding this method adds some new client-side code that sets up the components to do the validation.&amp;nbsp; This code looks like the following:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }&lt;br /&gt;window.mvcClientValidationMetadata.push({&amp;quot;Fields&amp;quot;:[{&amp;quot;FieldName&amp;quot;:&amp;quot;FirstName&amp;quot;,&amp;quot;ReplaceValidationMessageContents&amp;quot;:false,&amp;quot;ValidationMessageId&amp;quot;:&amp;quot;FirstName_validationMessage&amp;quot;,&amp;quot;ValidationRules&amp;quot;:[{&amp;quot;ErrorMessage&amp;quot;:&amp;quot;First name required.&amp;quot;,&amp;quot;ValidationParameters&amp;quot;:{},&amp;quot;ValidationType&amp;quot;:&amp;quot;required&amp;quot;}]},{&amp;quot;FieldName&amp;quot;:&amp;quot;LastName&amp;quot;,&amp;quot;ReplaceValidationMessageContents&amp;quot;:false,&amp;quot;ValidationMessageId&amp;quot;:&amp;quot;LastName_validationMessage&amp;quot;,&amp;quot;ValidationRules&amp;quot;:[{&amp;quot;ErrorMessage&amp;quot;:&amp;quot;Last name required.&amp;quot;,&amp;quot;ValidationParameters&amp;quot;:{},&amp;quot;ValidationType&amp;quot;:&amp;quot;required&amp;quot;}]},{&amp;quot;FieldName&amp;quot;:&amp;quot;Email&amp;quot;,&amp;quot;ReplaceValidationMessageContents&amp;quot;:false,&amp;quot;ValidationMessageId&amp;quot;:&amp;quot;Email_validationMessage&amp;quot;,&amp;quot;ValidationRules&amp;quot;:[{&amp;quot;ErrorMessage&amp;quot;:&amp;quot;Email&amp;nbsp; required.&amp;quot;,&amp;quot;ValidationParameters&amp;quot;:{},&amp;quot;ValidationType&amp;quot;:&amp;quot;required&amp;quot;}]}],&amp;quot;FormId&amp;quot;:&amp;quot;form0&amp;quot;,&amp;quot;ReplaceValidationSummary&amp;quot;:true,&amp;quot;ValidationSummaryId&amp;quot;:&amp;quot;validationSummary&amp;quot;});&lt;/p&gt;
&lt;p&gt;The client-side components contain the array of validators to apply to each form element.&amp;nbsp; It maps the property of the model object(which is equivalent to the ID of the control), mapping these to the validation rules to validate against.&amp;nbsp; For instance, you see a lot of required validators, which refers to a client-side implementation of each of the server-side component.&amp;nbsp; It also contains the error message to display to the user when the validation fails.&lt;/p&gt;
&lt;p&gt;The action method to get and post the data to the view looks like the same as our server-side validation example too.&amp;nbsp; Check the example below:&lt;/p&gt;
&lt;p&gt;[HttpGet]&lt;br /&gt;public ActionResult AjaxIndex()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;return View(new ValidationTestClass { FirstName = &amp;quot;Bob&amp;quot;, LastName = &amp;quot;Anderson&amp;quot;, Email = &amp;quot;&lt;a href="mailto:boba@test.com"&gt;boba@test.com&lt;/a&gt;&amp;quot; });&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;[HttpPost]&lt;br /&gt;public ActionResult AjaxIndex(ValidationTestClass val)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;if (!ModelState.IsValid)&lt;br /&gt;&amp;nbsp;&amp;nbsp;return View();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;return RedirectToAction(&amp;quot;Index&amp;quot;, &amp;quot;Home&amp;quot;);&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, the view that posts back fires the client-side validation first.&amp;nbsp; If it detects an error, the field is flagged with a message generated on the client-side.&amp;nbsp; If successful, the form posts back and processes the equivalent server-side validator, and if that check fires, the ModelState.IsValid property will evaluate to true.&lt;/p&gt;</description></item><item><title>Error with Client Validation</title><link>http://msmvps.com/blogs/bmains/archive/2010/03/26/error-with-client-validation.aspx</link><pubDate>Fri, 26 Mar 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1762436</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;We are about to talk about client validation soon, but if you already know about it, read&amp;nbsp;on; if not, wait until my next post and come back to this one :-).&amp;nbsp; I&amp;#39;m writing this now because I just found it.&amp;nbsp; If you use client validation, it&amp;#39;s important to ensure you enable client validation before the form gets rendered.&amp;nbsp; The extensions append a unique form ID to the form element, and thus, if you don&amp;#39;t enable validation, this imporant required form id doesn&amp;#39;t get set (is null) and raises a client-side error.&amp;nbsp; So be careful to the ordering, it should be in this manner:&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EnableClientValidation(); %&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;And everything will be alright :-)&lt;/p&gt;</description></item><item><title>Server Side Validation Updates in MVC 2</title><link>http://msmvps.com/blogs/bmains/archive/2010/03/26/server-side-validation-updates-in-mvc-2.aspx</link><pubDate>Fri, 26 Mar 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1762435</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;ASP.NET MVC 1 had features built in&amp;nbsp;that allowed you to validate content on the server-side, by specifying the elements to listen to validations for.&amp;nbsp; These features have been enhanced so that you can use a lambda to specify the field to validate.&amp;nbsp; Below is an illustration of these new features.&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.ValidationSummary(&amp;quot;The following errors have happened:&amp;quot;) %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;fieldset&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;legend&amp;gt;Form Values&amp;lt;/legend&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;First: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.FirstName) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.FirstName, &amp;quot;*&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;Last: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.LastName) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.LastName, &amp;quot;*&amp;quot;)%&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;Email: &amp;lt;%= Html.TextBoxFor(i =&amp;gt; i.Email) %&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;lt;%= Html.ValidationMessageFor(i =&amp;gt; i.Email, &amp;quot;*&amp;quot;)%&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Save&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/fieldset&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;Here we use a lamdba to specify the field to validate; the field to display in a textbox and the field to validate are both specified via lambdas, which will use the property name as the field ID.&amp;nbsp; When the view posts back, the model binder validates the posted input, checking for validation errors.&amp;nbsp; The validation errors are stored in the ModelState object, which has an IsValid property.&amp;nbsp; When errors occur, IsValid is false, allowing you to control what happens next.&amp;nbsp; Any errors are displayed inline as a span with the error text that&amp;#39;s passed in (the second parameter).&lt;/p&gt;
&lt;p&gt;Check out the post operation below to see how we process the input.&lt;/p&gt;
&lt;p&gt;[HttpGet]&lt;br /&gt;public ActionResult Index()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;return View(new ValidationTestClass { FirstName = &amp;quot;Bob&amp;quot;, LastName = &amp;quot;Anderson&amp;quot;, Email = &amp;quot;&lt;a href="mailto:boba@test.com"&gt;boba@test.com&lt;/a&gt;&amp;quot; });&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;[HttpPost]&lt;br /&gt;public ActionResult Index(ValidationTestClass val)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; if (!ModelState.IsValid)&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; return View();&lt;br /&gt;&lt;br /&gt;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return RedirectToAction(&amp;quot;New&amp;quot;);&lt;br /&gt;} &lt;/p&gt;
&lt;p&gt;Now we have a check; when the post back to the server has errors, we redirect back to the view so the user can correct the errors within the view.&amp;nbsp; The MVC framework retains the form collection so the user can correct the issues; you do not need to do anything to do this.&amp;nbsp; When the user gets it right, the form posts back, and the redirection takes place.&amp;nbsp; In case you didn&amp;#39;t know, the model binder has the ability to parse the form values by their name and automatically assign them to the object that is the parameter, in this class, a class with properties.&lt;/p&gt;
&lt;p&gt;So what is used to do the validation?&amp;nbsp; What you don&amp;#39;t see here is that a ModelMetadataProvider defines the rules for validating class instances.&amp;nbsp; By default, validation uses data annotations, and if you like, buddy classes, for validating classes.&amp;nbsp; So reflection extracts the attributes in the System.ComponentModel.DataAnnotations assembly, checks the data against these validation definitions, and then outputs the results in the UI.&amp;nbsp; Below is the class that represents the validations used for the above form.&amp;nbsp; Later on, we are going to look at the ModelMetadataProvider and how you can customize it, but for now, below is our test class:&lt;/p&gt;
&lt;p&gt;public class ValidationTestClass&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;[Required(ErrorMessage = &amp;quot;First name required.&amp;quot;)]&lt;br /&gt;&amp;nbsp;public string FirstName { get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;[Required(ErrorMessage = &amp;quot;Last name required.&amp;quot;)]&lt;br /&gt;&amp;nbsp;public string LastName { get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;[&lt;br /&gt;&amp;nbsp;Required(ErrorMessage = &amp;quot;Email&amp;nbsp; required.&amp;quot;),&lt;br /&gt;&amp;nbsp;DataType(DataType.EmailAddress, ErrorMessage = &amp;quot;Invalid Email&amp;quot;)&lt;br /&gt;&amp;nbsp;]&lt;br /&gt;&amp;nbsp;public string Email { get; set; }&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each property defines the type of rules that are validated, and the resulting error message if the validation fails.&amp;nbsp; Each validator can take property values to configure the type of validation, like the DataType attribute for the Email property.&amp;nbsp; The MVC framework takes these attribute definitions and validates these within the form.&lt;/p&gt;</description></item><item><title>Input Validation in MVC (1 and 2)</title><link>http://msmvps.com/blogs/bmains/archive/2010/03/19/input-validation-in-mvc-1-and-2.aspx</link><pubDate>Fri, 19 Mar 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1761999</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;In ASP.NET web forms, anytime an element within the form post contains an HTML tag (such as&amp;nbsp;a &amp;lt;div&amp;gt; or any other tag), the input that&amp;#39;s posted back to the server is validated, and an exception thrown, if that post contains any HTML tags within it.&amp;nbsp; This is request validation, and in web forms it can be enabled or disabled by setting the ValidateRequest property.&amp;nbsp; When turned off, the validation of data is blocked for the entire request; it is a feature that can&amp;#39;t be selective (in that you have to turn it on or off for the entire page, and doesn&amp;#39;t block individual controls only).&amp;nbsp; MVC has this capability too; take the following view:&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.BeginForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;p&amp;gt;&lt;br /&gt;&amp;nbsp;Name: &amp;lt;%= Html.TextArea(&amp;quot;Name&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;p&amp;gt;&lt;br /&gt;&amp;nbsp;Html: &amp;lt;%= Html.TextArea(&amp;quot;Html&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;lt;/p&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Save&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% Html.EndForm(); %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;if, in any of the two text areas, there are html tags within the posted data, an exception would be thrown in web forms, and can be thrown in MVC too.&amp;nbsp; To set this up, we use the ValidateInputAttribute at the class or method level, like the following:&lt;/p&gt;
&lt;p&gt;[HttpPost, ValidateInput]&lt;br /&gt;public ActionResult Validation(FormCollection values)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;ViewData[&amp;quot;IsValid&amp;quot;] = ModelState.IsValid;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;return View();&lt;br /&gt;}&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Because of&amp;nbsp;the attribute declarion, the input will be validated&amp;nbsp;on&amp;nbsp;posting the form data.&amp;nbsp; An error will occur and this action won&amp;#39;t be invoked because of the invalid input, preventing any malicious attack against the application.&amp;nbsp;&amp;nbsp;See more of the documentation&amp;nbsp;at: &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(VS.100).aspx"&gt;http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(VS.100).aspx&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>New Data Display Options in MVC 2</title><link>http://msmvps.com/blogs/bmains/archive/2010/03/18/new-data-display-options-in-mvc-2.aspx</link><pubDate>Thu, 18 Mar 2010 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1761899</guid><dc:creator>bmains</dc:creator><description>&lt;p&gt;ASP.NET MVC 2 has provided a lot more options for displaying the data to the end&amp;nbsp;user within your views.&amp;nbsp; Here are a few of the options you have:&lt;/p&gt;
&lt;p&gt;Html.DisplayFor(i =&amp;gt; i.FirstName) - DisplayFor renders the value of a property directly to the browser.&amp;nbsp; In this example, the FirstName property returns a string, which gets rendered.&amp;nbsp; What is &amp;quot;i&amp;quot; in this instance?&amp;nbsp; This is a reference to the Model that&amp;#39;s bound to the view in the @Page definition.&amp;nbsp; Remember that the Model is accessible as a part of the ViewDataDictionary class (accessible via ViewData property of the controller), and that views either store an object reference to the model, or a generic reference.&amp;nbsp; Using some of these new syntaxes was built to be more convenient with the generic forms&amp;nbsp;(the view inheriting from ViewPage&amp;lt;T&amp;gt; rather than ViewPage).&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.DisplayFor(i =&amp;gt; i.FirstName).ToHtmlString() %&amp;gt;&lt;br /&gt;&amp;lt;%= Html.DisplayFor(i =&amp;gt; i.LastName).ToHtmlString() %&amp;gt;&lt;br /&gt;&lt;br /&gt;DisplayFor can display a simple property within this model if you like, or it has the ability to display more complex types, as we&amp;#39;ll see more details about this soon.&lt;br /&gt;&lt;br /&gt;Html.DisplayForModel() - Renders a display view for the entire model object; by default, renders a label and display element for each property in the object.&amp;nbsp; It&amp;#39;s possible to completely customize the UI for this, using the new template feature in ASP.NET MVC 2 (more on this later too).&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.DisplayForModel() %&amp;gt;&lt;br /&gt;&lt;br /&gt;Html.DisplayText() - Displays a property value within the model referencing the value via a string; it take the name of a&amp;nbsp;property or expression to display data, and renders that data as text.&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.DisplayText(&amp;quot;Title&amp;quot;) %&amp;gt;&lt;/p&gt;
&lt;p&gt;Interesting thing about templates; the latest version of MVC includes the templating features&amp;nbsp;similar to what&amp;nbsp;you see in Dynamic Data.&amp;nbsp; You can create, in the controller or shared folder, displaytemplates and editortemplates folders that contains the templates for the display or edit versions of the templates for the data being displayed.&amp;nbsp; By default, simple properties get displayed using in-built templates created by Microsoft.&amp;nbsp; So, when the code to display the first name (shown above) executes, the string value being shown uses a built-in template to render the content.&amp;nbsp; Brad Wilson covered this in a blog post in great detail: &lt;a href="http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html"&gt;http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In these special folders, you can create&amp;nbsp;custom templates for simple data types (int, float, decimal, string, etc.), or&amp;nbsp;create templates for complex objects (business objects and such).&amp;nbsp; For instance, suppose you have a Contact class generated from a LINQ to SQL model.&amp;nbsp; We can create a specialized UI for displaying the data in a folder structure shown below.&lt;/p&gt;
&lt;p&gt;Views&lt;br /&gt;&amp;nbsp;&amp;nbsp; Shared&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;DisplayTemplates&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Contact.ascx (partial view)&lt;/p&gt;
&lt;p&gt;Anything displaying a Contact LINQ to SQL object will use this contact UI form, which has the following definition:&lt;/p&gt;
&lt;p&gt;&amp;lt;%@ Control Language=&amp;quot;C#&amp;quot; &lt;br /&gt;&amp;nbsp;Inherits=&amp;quot;System.Web.Mvc.ViewUserControl&amp;lt;ASPMVC2.DataAccess.DataModels.Contact&amp;gt;&amp;quot; %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;strong&amp;gt;&lt;br /&gt;Custom Contact Template&lt;br /&gt;&amp;lt;/strong&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Model.FirstName %&amp;gt;&lt;br /&gt;&amp;lt;%= Model.LastName %&amp;gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;So how does the DisplayFor method know which template to use?&amp;nbsp; The&amp;nbsp;framework uses the template similar to the following:&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.DisplayFor(i =&amp;gt; i.Contact, &amp;quot;ContactTemplate&amp;quot;); //ContactTemplate matches the user control name&lt;/p&gt;
&lt;p&gt;By default, it will do a name match of the property to the template name, but you can also customize the process by specifying the name manually as illustrated above.&amp;nbsp; This explicit reference&amp;nbsp;searches for the template within either the ~/Views/&amp;lt;Controller&amp;gt;/DisplayTemplates/ContactTemplate.ascx or the ~/Views/Shared/DisplayTemplates/ContactTemplate.ascx file.&amp;nbsp; Now, we have one centralized template for the Contact type, reusable across the entire application.&lt;/p&gt;</description></item></channel></rss>