Jerry
Jerry

Reputation: 435

How to create custom scaffolding template in asp.net mvc3?

ASP.NET MVC 3 can generate scaffold code for us(controllers and views). The generated views by default use div and css layout.
But sometimes I need table layout views rather than div (as in default mode). How to generate table layout views like the following code? I also need the column number in a table row can be customerized.
My Question is: how to make asp.net mvc automatically generate talbe layout views for me. And more generally, how to modify the scaffold code template?

    <table>
    <tr>    
    <td class="editor-label">
        Id
    </td>
    <td class="editor-field">
        @Html.EditorFor(model => model.Id )
        @Html.ValidationMessageFor(model => model.Id)
    </td>
    <td class="editor-label">
        name
    </td>
    <td class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </td>
    </tr>
    <tr> ... other fields go here ...  </tr>
    </table>

Upvotes: 0

Views: 3162

Answers (2)

Jerry
Jerry

Reputation: 435

Some other guys also asked this question. Because I use different key words in my question, I didn't find these questions until now.
a similar question on stackoverflow
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
ASP.Net MVC - T4 Fun
MvcScaffolding: Overriding the T4 Templates

Upvotes: 0

Tommy
Tommy

Reputation: 39817

I have never taken the time to override the T4 templates, but there are some blog post about the exact way to get to the templates and create/modify with your own layouts. The best example I could find is this blog post by Steve Sanderson

http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates/

It appears that you will need to override the Scaffolder "View" and the Create, CreateOrEdit, Edit templates. Once you run the command specified in the above post in the PM console line (see below)

Scaffold CustomTemplate View Edit

Then it will create a copy in your project which you can then modify and use as you wish.

Upvotes: 1

Related Questions