Sunday, June 12, 2011

MVC 3 Razor Editor Template

Editor Template is very useful future in MVC3 Framework.With Editor Template we can create template for model and  It  can be access easily in the application (like user controller in
So I'm going to create Editor Template for Book Model in my application.

public class Book
public int BookId { get; set; }
public string BookName { get; set; }
public string Description { get; set; }
Then I'm going to create folder  as EditorTemplates in Shared folder  and add new view Book.cshtml as following.(The Name of the Template should be same as Class Name)
@model MySimpleEditorTemplate.Models.Book

@Html.DisplayFor(p => p.BookId) @Html.EditorFor(p => p.BookId)
@Html.DisplayFor(p => p.BookName)@Html.EditorFor(p => p.BookName)
@Html.DisplayFor(p => p.Description)@Html.EditorFor(p => p.Description)

Then in view page simply we can add editor for book as

@Html.EditorFor(model => model.Book) 
then it will display above block

1 comment: