How to scaffold views with custom ViewModel?

Mar 16, 2012 at 11:45 PM

I use custom ViewModels in all my views.  Is there a way of scaffolding custom view templates that pick up my viewmodel?  At the moment it doesn't detect it unless I'm doing something wrong.

Here's an example of my Edit Razor view:

@model ESMvcDemo.Mvc.ViewData.CategoriesViewData

@{
    ViewBag.Title = @Model.PageTitle;
}

<h2>@Model.PageTitle</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Categories</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Categories.CategoryName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Categories.CategoryName)
            @Html.ValidationMessageFor(model => model.Categories.CategoryName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Categories.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Categories.Description)
            @Html.ValidationMessageFor(model => model.Categories.Description)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

This line in the Index.cs.t4 always returns null:

<# var viewDataType = (EnvDTE.CodeType) Model.ViewDataType; #>

Any suggestions on how to create a custom view template for this?

Thanks.