문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top