MVC: cannot get the syntax right for the editor template to work
I have a strongly typed view in which the first line is;
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Employee.Master"
Inherits="System.Web.Mvc.ViewPage<IEn开发者_StackOverflow中文版umerable<SHP.Models.AnnualLeaveBooked>>" %>
To avoid Id collisions I want to display the collection in a grid so I use an editor template (one of the fields gets edited). So in my code I put in the line;
<%: Html.EditorFor(x => x) %>
And then in the EditorTemplate sub folder I put in this template;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.Models.AnnualLeaveBooked>" %>
<tr>
<td>
<%: String.Format("{0:g}", Model.AnnualLeaveDate) %>
</td>
<td>
<%: Model.ApprovedStatus %>
</td>
<td>
<%: Html.CheckBoxFor(model => model.CancelFlag) %>
<%: Html.HiddenFor(model => model.AnnualLeaveBookedId) %>
</td>
</tr>
The problem I am having is that the EditorFor command does not seem to recognise the EditorTemplate. What am I doing wrong? Incidently I would be suprised if x => x is wrong, there is probably a better way to express this.
Your editor template should be: ~/Views/Home/EditorTemplates/AnnualLeaveBooked.ascx
where Home
is the name of the controller. If it is a global editor template you could also put it in ~/Views/Shared/EditorTemplates/AnnualLeaveBooked.ascx
. Notice the name of the editor template: it should be the same as your type name (AnnualLeaveBooked.ascx
).
Also instead of:
<%: Html.EditorFor(x => x) %>
you could:
<%: Html.EditorForModel() %>
Finally if you want to name your editor template with some custom name like ~/Views/Home/EditorTemplates/Foo.ascx
you could specify this custom name:
<%: Html.EditorForModel("Foo") %>
精彩评论