static void

ASP MVC

Links

In MVC3, use DependencyResolver.SetResolver to put in your IOC framework

Actions

Action Results

To return simple values (such as an int) add [NoAction] attribute.

Partial Actions

An Html.Partial can just call a partial view/user control; Html.Action (and Html.RenderAction) calls a controller which in turn calls a partial view. In other words, Html.Action allows the view to do processing- the view calls a controller to call a view.

<div>@Html.Action("CategorySummary", "Category", Model)</div>

In CategoryController. Mark as [ChildActionOnly] if required. ControllerContext.IsChildAction can detect if within an Action.

[ChildActionOnly]
public ActionResult CategorySummary(CategoryModel category)
{
    //(ControllerContext.IsChildAction)
    return PartialView(category);
}

Action Filters

Asp.Net

[OutputCache] Duration, VaryByParams etc
[Authorize] Any authenticated user - specifics with Users = "", Roles = ""
[ValidateAntiForgeryToken] In conjunction with Html.AntiForgeryToken()
[HandleError] Error trapping, It goes to the default Shared/Errors.cshtml.
  • Attribute on method (specific action) or class (=all controller actions)
  • CustomErrors=RemoteOnly by default
  • If you don't use HandleError, it falls through to global.asax Application_Error
  • Alternatively (in MVC3) apply global filters

HandleErrors in GlobalFilters

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    //multiple filters are applied with ascending Order (default = -1)
 
    //database errors
    filters.Add(new HandleErrorAttribute
    {
        ExceptionType = typeof(System.Data.Common.DbException),
        View = "DatabaseError", //-> Shared/DatabaseError.cshtml
        Order = 1
    });
    //generic errors (default order is -1)
    filters.Add(new HandleErrorAttribute { Order = 2 });
}
 
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
 
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

Error views have no controller. The model is @model System.Web.Mvc.HandleErrorInfo

Action Input Binding

You can still access Request.Form, or accept a FormCollection, but AspMVC's DefaultModelBinder is quite good at mapping the HttpPosted fields (and Route data) into simple model classes.

You can use simple arguments (which can have defaults), with UpdateModel (or TryUpdateModel) - which can take an interface (as well as inclusions/ exclusions).

[HttpPost]
public ActionResult Edit(int categoryId, string categoryName)
{
    var category = new CategoryModel();
    UpdateModel(category, new[] { "CategoryName", "CategoryId" });
 

...Or...

[HttpPost]
public ActionResult Edit(CategoryModel category)
 

BindAttribute

This applies to the individual parameters - or to your model class.

[HttpPost]
public ActionResult Edit(
    [Bind(Include = "CategoryName,CategoryId")]
    CategoryModel category)

Custom ModelBinders