MVC3 Razor 模板 - EditorForModel

发布于 2024-11-02 17:47:24 字数 2002 浏览 0 评论 0原文

所以,我正在关注 本文自定义 Html.EditorForModel 模板。让它工作 - 很好。

我尝试将其转换为 Razor (Object.cshtml) 并得到:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
Compiler Error Message: CS0115: 'ASP._Page_Views_Shared_EditorTemplates_Object_cshtml.Execute()': no suitable method found to override

Source Error:


Line 44:         }
Line 45:         
Line 46:         public override void Execute() {
Line 47: 
Line 48: 

代码

@inherits System.Web.Mvc.ViewUserControl
@{  var count = 0; }
@if (ViewData.TemplateInfo.TemplateDepth > 1) { 
  @ViewData.ModelMetadata.SimpleDisplayText
} 
else { 
  <table class="form">
    <tr>
    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
      if(prop.HideSurroundingHtml) {
        @Html.Editor(prop.PropertyName) 
      } 
      else { 
        if(count == 2) {
          count = 0;
          @:</tr><tr>
        } 
        else {
          count++;
        }
        <td>hi
          <div class="editor-label" style="text-align: right;">
            @prop.IsRequired ? "*" : "" 
            @Html.Label(prop.PropertyName) 
          </div>
        </td>
        <td>
          <div class="editor-field">
            @Html.Editor(prop.PropertyName) 
            @Html.ValidationMessage(prop.PropertyName, "*") 
          </div>
        </td>
      } 
    } 
    </tr>
  </table>
} 

这是我无法猜测的

。 “有趣的是”当模板被称为“_Object.cshtml”时, @Html.EditorForModel("~/Views/Shared/EditorTemplates/_Object.cshtml") 被完全忽略并使用默认模板,所以知道为什么必须被称为“对象”将是一个很好的了解。

So, I'm following this article to customise the Html.EditorForModel template. Had it working - fine.

I tried converting it to Razor (Object.cshtml) and get:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
Compiler Error Message: CS0115: 'ASP._Page_Views_Shared_EditorTemplates_Object_cshtml.Execute()': no suitable method found to override

Source Error:


Line 44:         }
Line 45:         
Line 46:         public override void Execute() {
Line 47: 
Line 48: 

Here's the code

@inherits System.Web.Mvc.ViewUserControl
@{  var count = 0; }
@if (ViewData.TemplateInfo.TemplateDepth > 1) { 
  @ViewData.ModelMetadata.SimpleDisplayText
} 
else { 
  <table class="form">
    <tr>
    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
      if(prop.HideSurroundingHtml) {
        @Html.Editor(prop.PropertyName) 
      } 
      else { 
        if(count == 2) {
          count = 0;
          @:</tr><tr>
        } 
        else {
          count++;
        }
        <td>hi
          <div class="editor-label" style="text-align: right;">
            @prop.IsRequired ? "*" : "" 
            @Html.Label(prop.PropertyName) 
          </div>
        </td>
        <td>
          <div class="editor-field">
            @Html.Editor(prop.PropertyName) 
            @Html.ValidationMessage(prop.PropertyName, "*") 
          </div>
        </td>
      } 
    } 
    </tr>
  </table>
} 

I'm out of guesswork.

"Interestingly" when the template is called "_Object.cshtml", @Html.EditorForModel("~/Views/Shared/EditorTemplates/_Object.cshtml") is completely ignored and the default template is used, so knowing why is has to be called "Object" would be a nice to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2024-11-09 17:47:24

尝试删除第一行(@inherits 内容):

@{ var count = 0; }
@if (ViewData.TemplateInfo.TemplateDepth > 1) { 
  @ViewData.ModelMetadata.SimpleDisplayText
} 
else { 
  <table class="form">
    <tr>
    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
      if(prop.HideSurroundingHtml) {
        @Html.Editor(prop.PropertyName) 
      } 
      else { 
        if(count == 2) {
          count = 0;
          @:</tr><tr>
        } 
        else {
          count++;
        }
        <td>hi
          <div class="editor-label" style="text-align: right;">
            @if (prop.IsRequired)
            {
                @:*   
            }
            @Html.Label(prop.PropertyName) 
          </div>
        </td>
        <td>
          <div class="editor-field">
            @Html.Editor(prop.PropertyName) 
            @Html.ValidationMessage(prop.PropertyName, "*") 
          </div>
        </td>
      } 
    } 
    </tr>
  </table>
} 

另请注意我重写 @prop.IsRequired 测试的方式。

Try removing the first line (the @inherits stuff):

@{ var count = 0; }
@if (ViewData.TemplateInfo.TemplateDepth > 1) { 
  @ViewData.ModelMetadata.SimpleDisplayText
} 
else { 
  <table class="form">
    <tr>
    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
      if(prop.HideSurroundingHtml) {
        @Html.Editor(prop.PropertyName) 
      } 
      else { 
        if(count == 2) {
          count = 0;
          @:</tr><tr>
        } 
        else {
          count++;
        }
        <td>hi
          <div class="editor-label" style="text-align: right;">
            @if (prop.IsRequired)
            {
                @:*   
            }
            @Html.Label(prop.PropertyName) 
          </div>
        </td>
        <td>
          <div class="editor-field">
            @Html.Editor(prop.PropertyName) 
            @Html.ValidationMessage(prop.PropertyName, "*") 
          </div>
        </td>
      } 
    } 
    </tr>
  </table>
} 

Also notice the way I rewrote the @prop.IsRequired test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文