MVC3 Razor 模板 - EditorForModel
所以,我正在关注 本文自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除第一行(
@inherits
内容):另请注意我重写
@prop.IsRequired
测试的方式。Try removing the first line (the
@inherits
stuff):Also notice the way I rewrote the
@prop.IsRequired
test.