web 2.0

Adding Export Capabilities to the Razor WebGrid

When MVC first came out I really missed having a native grid control. Now a few years later, we have grid support again! In case, you didn’t know the Razor WebGrid was included in the System.Web.Helpers assembly which shipped with ASP.NET MVC 3. Over the years, I have sampled a variety of grid controls such as jqGrid, the Infragistics grid control and the MvcContrib grid. Each grid control has its benefits and disadvantages but I think the new WebGrid offers a clean API which makes development tasks simple. Here is a screenshot of what the grid looks like: To display a grid on a razor view page, you basically create a new grid object, assign some properties and then call the GetHtml helper method. Here is an example: @{ var grid = new WebGrid(canPage: true, canSort: true, rowsPerPage: Model.PagingInfo.PageSize, ajaxUpdateContainerId: "grid"); grid.Bind(Model.Data, rowCount: Model.PagingInfo.TotalItems, autoSortAndPage: false); } @grid.GetHtml( column... [More]

Tags:

ASPNet | MVC | Tech

Building MVC Select Lists The Easy Way

It is common to have a view model that contains a list of select list items ( IEnumerable ). However, I get a little annoyed when I see a select list item getting initialized like this: Widgets = db.Widgets.FindAll().Select(x => new SelectListItem() { Text = x.Name, Value = x.Value }); Although this is legal syntax its just a little bit too ugly for my liking. In an effort to make life simpler I came up with the following helper class: public static class SelectListFactory { public static IEnumerable<SelectListItem> BuildList<T>(IEnumerable<T> items, Func<T, SelectListItem> func) { return items.Select(func); } public static IEnumerable<SelectListItem> BuildList<T>( IEnumerable<T> items, Func<T, string> text, Func<T, string> value = null, Func<T, Boolean> selected = null) { return items.Select(x => new SelectListItem{ ... [More]

Tags: ,

ASPNet | MVC | Tech

MVC–Showing a setup screen on the first run

Recently, I participated in a project where I helped to build a website in Orchard. One of the things that I admire (and there are many) about Orchard is the setup screen that appears the first time you run the site. It’s a nice touch and really makes the site easy to configure. No need to tweak a web.config, create a database or read a 20 page setup guide on how to get started. You just fill out the form and the site is ready… Of course, I wanted to build something similar for my open source project WeBlog. First, I tried to reverse engineer the orchard code. However, orchard uses a lot of dynamic types and things are heavily abstracted so it was hard to re-use their code for my purposes. Next, I tried to Google for a solution but the only thing I could find was this stack overflow post. My first thought was to create a base controller class and override the OnActionExecuting method. In that method I could force a redirect if the site was not configured yet. However, I ... [More]

Building a Nuget Server with gold plating

Last week, Phil Haack wrote a post on how to build a NuGet Server. A NuGet server is really easy to build because 90% of the work is done by running a NuGet command. Just for fun, I decided to add some extra functionality to my NuGet Server such as a package upload form and a formatted list of packages.  In preparing my NuGet server, I followed Phil’s direction to a tee except for the fact that I used a “empty MVC 3 app with Razor support” instead of using a “ASP.NET Empty Web Application”. Out of the box, you will get a barebones, but fully functional NuGet server that provides the package list in Atom Format. For each package in the repository you will get access to properties such as the version number, license information, tags and etcetera. However, since I am not a fan of reading XML I thought it would be useful to transform that XML into HTML using XSLT. This way I can see the list of available packages without opening up Visual Studio. XSLT is one of those technologies t... [More]

MVC Tip - An easy way to deal with those NotFound Errors

My first introduction to MVC was the Nerd Dinner tutorial. From that tutorial I adopted the concept of redirecting the user to a “NotFound” page when a item was requested that does not exist. Here is an example of a controller action that returns the “NotFound” view: public ActionResult Activate(int id) { User user = _repository.FindById(id); if (user == null) return View("NotFound"); //other code.... return View("Activated"); }   However, a problem may come into play when you try to encapsulate logic in a model class. For example, let's suppose I have a class named EditUserViewModel which loads the details for a user account. The constructor takes a repository and an integer value which represents the primary key used in the database. public class EditUserFormViewModel { public EditUserFormViewModel( IUserRepository repository, int id ) : base() { var user = repository.FindById(id); ... [More]