Question

I am using the new Razor view engine with ASP.NET MVC and would like to know how to modify the editor template master page in a similar way to how it is done in this blog post. Is there any examples on how to do this with Razor?

Was it helpful?

Solution

You could achieve the same thing with the Razor view engine.

Model:

public class MyViewModel
{
    public string Value { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Value = "foo"
        };
        return View(model);
    }
}

Views:

~/Views/Home/Index.cshtml:

@model MyApp.Models.MyViewModel

@{ Html.BeginForm(); }

    @Html.EditorFor(x => x.Value)
    <input type="submit" value="OK" />

@{ Html.EndForm(); }

~/Views/Home/EditorTemplates/Template.cshtml:

<p>Some text before template</p>
@RenderBody()
<p>Some text after template</p>

~/Views/Home/EditorTemplates/string.cshtml:

@model System.String
@{
    Layout = "~/Views/Home/EditorTemplates/Template.cshtml";
}
<div>@Html.TextBoxFor(x => x)</div>

Notice how the string editor template has been customized and the Template.cshtml used as master layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top