Improving the world one post at a time
ASP.Net MVC 2
After looking at FluentValidation, I decided that most of what I needed was a way to do server-side validation of more complicated rules. While this is something FluentValidation does, it seemed like more than I needed so I set out to create a simple extension method to do what I needed. While it is still not necessarily perfect, it is simple and works. 1: public static void Validate<T, TResult>(this Controller controller, T model, Expression<Func<T, TResult>> expression, Func<TResult, ......
SharePoint is very handy as a repository for data. There are things (particularly in the intranet world) which really don't need a dedicated database or application and SharePoint is a great enabler. However, if you want to build a lightweight, standards-friendly web page using SharePoint, you have a challenge ahead of you. Here is the problem I was trying to solve: I have a SharePoint site that serves as the home page for an organization. I needed to be able to pull in various content from other ......
Recommendation: Don't use the same action names for get and (Ajax) post.I am always amazed at how bad Microsoft's code examples are. Code generated using the default templates in Visual Studio is not much better. To find out, create an empty project (pick your favorite type) and run static code analysis or FxCop and see how many warnings you see. For some real fun try running StyleCop. The default templates (and therefore commonly-used standard practices) are also not very good in my opinion. Consider ......
While you can do most common tasks by simply passing expressions through to existing HtmlHelper extension methods, you may stumble on something where you want to evaluate the lambda expression on your own. While this may seem daunting at first, the MS folks have made it pretty simple. All you have to do is this: ModelMetadata modelMetaData = ModelMetadata.FromLambdaExp... htmlHelper.ViewData); This assumes that htmlHelper and epxression are parameters of your extension method which ......
Consider the following code:<%= Html.DropDownList("State", new SelectList(new string[] { "","AK","AL","AR","AS","AZ"... "IL","IN","KS","KY","LA","M... "OH","OK","OR","PA","PR","P... %> Simple enough, right? Display a dropdown list with a value for each state. There is only one problem. The resulting ......
MVC 2 provides a GREAT feature for dealing with enumerable types. Let's say you have an object with a parent/child relationship and you want to allow users to modify multiple children at the same time. You can simply use the following syntax for any indexed enumerables (arrays, generic lists, etc.) and then your values will bind to your enumerable model properties. 1: <% using (Html.BeginForm("TestModelP... "Home"))2: { %>3: <table>4: <tr><th>ID</... ......
Most people who are really into ASP.Net MVC probably enjoy its test-driven nature or "unit-testability". This is a key benefit of the asynchronous model used by the MVC framework when action results are returned. Another benefit of this is in memory management. I am working on creating a large document which is to be available both online and offline and consists of several hundred (potentialy more than a thousand) items. The items are stored in a tree structure where each item has children and children ......
I wrote a simple pager AjaxHelper extension. You can convert this to an HtmlExtension method by just removing the AjaxOptions and swapping out the AjaxHelper with HtmlHelper. This assumes that your parameter for the page name is "Page". 1: public static string Pager(this AjaxHelper ajaxHelper, int page, int pageSize, int count, int pages, string actionName, string controllerName, object routData, AjaxOptions ajaxOptions) 2: { 3: StringBuilder sb = new StringBuilder(); 4: int pageCount = count / pageSize ......
I am a firm believer that, regardless of the framework you use, if the framework is good it provides you all of the things you need. I believe ASP.Net MVC 2 is good and therefore provides me everything I need. This means I don't need to write a significant amount of custom UI code (jQuery, MVC Ajax, etc.) to do things that should be considered "standard". One of these pretty standard things submitting a form via Ajax. MVC provides for this very well with Ajax.BeginForm. The contents of the form are ......
I have seen several posts on how to use the ASP.Net chart controls with MVC and they all involved some sort of hybrid web forms approach. I have a simple solution which is 100% MVC. Here is a summary of the process: Create an action to render the chart. Create an action to render the image map. Use a normal IMG tag to render the chart image. Use Html.RenderAction to render the image map. The most simple implementation without drill-down would look something like this: public ActionResult Chart(int? ......
Full MVC Archive