random techno tidbits
Posts tagged asp.net mvc
Organizing view models in ASP.NET MVC (Part 3 of 3)
Mar 12th
This is the second part of Organizing view models in ASP.NET MVC (see Part 1, Part 2).
In this part I’ll give a sample of designing view models for handling forms in MVC. Suppose that we need to create a controller and stuff around it to handle form like the following.
For the sake of demo simplicity I keep the form simple:

More >
Organizing view models in ASP.NET MVC (Part 2 of 3)
Feb 1st
This is the second part of Organizing view models in ASP.NET MVC (see Part 1).
So, designing classes for your view models inevitably brings up some new concerns, like how should the Layout view models be implemented (master pages in terms of WebForms view engine).
From my experience, you should create an abstract class like SharedLayoutViewModel with all of the data properties required by your _Lauout.cshtml. Then, make all of the view models of views that use this layout inherit this class. You can use MVC Result filters to fill this model with data in a single place of your app, or, like I do, override the OnResultExecuting method in the common base class for your controllers, and fill it there.
To make your view models easy to find, organize them in a similar fashion as you do for views. I prefer putting all the view models in a single namespace (though in different project directories) and having full name as its class name like ProductsCreateViewModel (if you’re using ReSharper, set NamespaceProvider = False). This way I avoid need of explicit namespace reference in views and controllers. So my final layout looks like this:

In the next part I’ll give a sample of a simple form implemented using the described approach.
ASP.NET MVC bug binding collection of DropDownList?
Jan 26th
Recently, while experimenting with strongly-typed form helpers of MVC, I found a bug.
I posted a question at SO, but with no luck so far.
Generally, looks like DropDownFor is broken when trying to bind collection of form drop downs.
I have a workaround in form of using non-typed helper, but anyway hope to get things sorted out with strongly-typed one.
Organizing view models in ASP.NET MVC (Part 1 of 3)
Jan 12th
The internet is filled up with video tutorials like “Creating wiki using ASP.NET MVC in 15 minutes”. The problem of these tutorials is that they do not cover real life scenarios. They tend to use ViewData and ViewBag to transfer data into View. They use non-typed Html.TextBox() helper methods for generating forms. They use controller method parameters to transfer form data back to the controller one by one. Or, more worse, they directly put ORM entity into controller method parameters, which can lead to huge problems when practiced by a non-experienced developer.
So, such “rapid” approaches can be attractive for the person, whose job is to create such video tutorials (probably trying to compete with the famous RoR one).
But in a real life applications, you’ll probably want to use strongly-typed models, strongly-typed model-aware Html.TextBoxFor(m => ..) helpers for generating forms, and get strongly-typed POST data into controller method. In other words, you need acceptable maintainability for your projects in MVC.
So here are the rules to remember, if you’re going to develop with real MVC.
- Views to ViewModels is a one-to-one relationship. Define separate ViewModel class for every single View.
- View dictates ViewModel structure, not the controller.
- ViewModles are dummy DTOs, with no logic in them.
- Forget about calling
Request.IsAuthenticatedand similar in your views. ViewModel should contain ALL the data required to render the view.
In my next post I’ll describe how you can use strongly-typed models in generic view layouts (master pages in MVC2). See Part 2.
Razor: Html.RenderPartial vs Html.Partial, Html.RenderAction vs Html.Action – what one should use?
Dec 29th
This question can be confusing for people starting using ASP.NET MVC3.
Today, we have two ways to render partial and action stuff:
@Html.Partial("Details") @Html.Action("Index", "Home")
and
@{ Html.RenderPartial("Details"); } @{ Html.RenderAction("Index", "Home"); }
So what would you go with?
More >
MVC Recipe: Using strongly-typed html helpers in @helper
Jul 20th
Razor is a brand new view engine for MVC. I’ll describe a method to use strongly-typed helpers like TextBoxFor(m=>m.FirstName) in its reusable @helper blocks
If you never heard about it, then read on ScottGu blog.
Suppose you have a checkout model that has two addresses – billing and shipping:
public class CheckoutInfoForm { public CheckoutAddressForm BillingAddress { get; set; } public CheckoutAddressForm ShippingAddress { get; set; } }
While each address has model like this:
public class CheckoutAddressForm { public string FirstName { get; set; } public string LastName { get; set; } public string Address1 { get; set; } // and so on }
How would you generate address fields for both and reuse the markup?
More >