【发布时间】:2023-03-30 15:22:01
【问题描述】:
我正在使用带有 ASP.NET MVC 的新 Razor 视图引擎,并且想知道如何以与 in this blog post 类似的方式修改编辑器模板母版页。有没有关于如何使用 Razor 执行此操作的示例?
【问题讨论】:
标签: c# asp.net-mvc razor
我正在使用带有 ASP.NET MVC 的新 Razor 视图引擎,并且想知道如何以与 in this blog post 类似的方式修改编辑器模板母版页。有没有关于如何使用 Razor 执行此操作的示例?
【问题讨论】:
标签: c# asp.net-mvc razor
您可以使用 Razor 视图引擎实现相同的目标。
型号:
public class MyViewModel
{
public string Value { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
Value = "foo"
};
return View(model);
}
}
观看次数:
~/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>
注意string 编辑器模板是如何定制的,Template.cshtml 用作主布局。
【讨论】: