ASP.Net C#MVC EditorFor模板没有从主剃刀视图调用

Mar*_*ark 5 c# asp.net asp.net-mvc razor asp.net-mvc-3

我正在尝试为重复的类(Offer)设置EditorTemplate - 但是,我无法让EditorTemplate显示.就好像它在EditorTemplates文件夹中找不到它一样:

我的ViewModel是:

    public class CreateViewModel
    {
    public IList<OfferVM> Offers { get; set; }
    public OfferVM Header
    {
        get
        {
            return Offers.FirstOrDefault();
        }
    }
}

public class OfferVM
{
    public int RoomTypeId { get; set; }
    public int PropertyId { get; set; }
    public string RoomTypeName { get; set; }
    public string Layout { get; set; }
    public decimal RoomRate { get; set; }
    public string Inclusions { get; set; }
    public string Property { get; set; }
    public bool IncludeInOffer { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的控制器获取代码是:

    //
    // GET: /Offer/Create

    public ActionResult Create()
    {

        var roomtypes = db.RoomTypes.Include(r => r.Property).ToList();
        var vm = new CreateViewModel();
        vm.Offers = Mapper.Map<IList<RoomType>, IList<OfferVM>>(roomtypes);
       return View(vm);
    }
Run Code Online (Sandbox Code Playgroud)

控制器工作正常 - 我可以看到它填充在VS.

我的Create.cshtml代码是:

@model FGBS.ViewModels.CreateViewModel
@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.Bootstrap().Begin(new Form().Type(FormType.Horizontal))) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend></legend>


<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.RoomTypeId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PropertyId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.RoomTypeName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Layout)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.RoomRate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Inclusions)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Property)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IncludeInOffer)
    </th>
    <th></th>
    </tr>

      @Html.EditorFor(x => x.Offers) 

    </table>
Run Code Online (Sandbox Code Playgroud)

然后在我的Views/Shared/EditorTemplates文件夹中,我有我的Offers.cshtml文件:

@model FGBS.ViewModels.OfferVM

@{
    Layout = null;
}
  <tr>
    <td>
        @Html.EditorFor(model => model.RoomTypeId)
        @Html.ValidationMessageFor(model => model.RoomTypeId)
    </td>


    <td>
        @Html.EditorFor(model => model.PropertyId)
        @Html.ValidationMessageFor(model => model.PropertyId)
    </td>


    <td>
        @Html.EditorFor(model => model.RoomTypeName)
        @Html.ValidationMessageFor(model => model.RoomTypeName)
    </td>


    <td>
        @Html.EditorFor(model => model.Layout)
        @Html.ValidationMessageFor(model => model.Layout)
    </td>


    <td>
        @Html.EditorFor(model => model.RoomRate)
        @Html.ValidationMessageFor(model => model.RoomRate)
    </td>


    <td>
        @Html.EditorFor(model => model.Inclusions)
        @Html.ValidationMessageFor(model => model.Inclusions)
    </td>


    <td>
        @Html.EditorFor(model => model.Property)
        @Html.ValidationMessageFor(model => model.Property)
    </td>


    <td>
        @Html.EditorFor(model => model.IncludeInOffer)
        @Html.ValidationMessageFor(model => model.IncludeInOffer)
  </td>

    </tr>
Run Code Online (Sandbox Code Playgroud)

SS

但是,当我运行代码时,我没有收到任何错误,但我的模板没有显示.

我已经尝试将模板更改为:

<tr><td>test</td></tr>
Run Code Online (Sandbox Code Playgroud)

但它仍然没有显示 - 这表明EditorFor没有"查找"editortemplates文件夹中的Offers.cshtml文件.

我也尝试在Offers.cshtml中添加一个断点,但是VS并没有在它内部停止,这意味着它没有被击中.

我的设置或Offers.cshtml文件的位置有什么问题吗?

谢谢,

标记

ana*_*der 12

为未来的访客提供答案:

正如指出的这另一个问题,默认情况下,EditorFor帮助使用其名称类型的名称相匹配正在编辑的模板.所以这里,你的模板没有被使用,因为它的名字Offers与viewmodel类型不匹配OfferVM.另一种方法是使用属性标记您想要EditorFor[UIHint("TemplateName")]属性,TemplateName编辑器模板的名称在哪里,在本例中为Offers.