开发者

Modify the Template Helpers MasterPage with Razor

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 po开发者_运维技巧st. Is there any examples on how to do this with Razor?


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.


ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜