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]

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]

ASP.NET MVC Unit Testing with UpdateModel and TryUpdateModel

Last week I was in the middle of re-writing an old MVC application. The app was written during the early days of MVC, and as a consequence it was a playground for learning and experimentation. As a developer, I am continuously learning new tricks so its’ no surprise that when I look at the code I wrote a few years ago I often refactor it. For example, when I started out with ASP.NET MVC I did not fully understand how the model binders worked. Unfortunately, this meant that some of my controller actions ended up looking like this: [HttpPost] public ActionResult Edit( Guid id, FormCollection form ) { var foo = new SomeModel(); foo.Id = id; foo.Name = form["Name"]; foo.Date = DateTime.Parse(form["EffectiveDate"]); foo.Description = form["Description"]; .... if (ModelState.IsValid) { ...Save and redirect } return View(model); } Yuck! Referencing form collections in your controlle... [More]