【问题标题】:Modify the Template Helpers MasterPage with Razor使用 Razor 修改模板助手 MasterPage
【发布时间】:2023-03-30 15:22:01
【问题描述】:

我正在使用带有 ASP.NET MVC 的新 Razor 视图引擎,并且想知道如何以与 in this blog post 类似的方式修改编辑器模板母版页。有没有关于如何使用 Razor 执行此操作的示例?

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:
    【解决方案2】:

    您可以使用 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 用作主布局。

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多