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.
Try removing the first line (the
@inheritsstuff):Also notice the way I rewrote the
@prop.IsRequiredtest.