2013-07-03 50 views
3

我试图为重复类(提供)设置EditorTemplate - 但是,我无法让EditorTemplate显示。这是因为如果它只是没有在EditorTemplates文件夹中找到:ASP.Net C#MVC EditorFor模板不被从主剃刀视图调用

我的视图模型为:

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; } 
    } 

我的控制器获取的代码是:

// 
    // 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); 
    } 

控制器正常工作 - 我可以看到它填充在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> 

然后在我的意见/共享/ 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> 

ss

然而,当我运行代码,我没有错误,但我的模板不显示。

我试图改变模板只是有:

<tr><td>test</td></tr> 

但它仍然不显示 - 这表明了EditorFor没有“发现”在editortemplates文件夹中的文件Offers.cshtml。

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

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

谢谢

马克

+0

不理我请的类型(@model型) - 改变了我对EditorTemplate和OfferVM.cshtml它的工作! – Mark

回答

10

为未来游客的答案:

this other question指出的,默认情况下,EditorFor助手使用其名称和类型名称匹配的模板正在编辑。因此,在这里,您的模板未被使用,因为其名称Offers与视图模型类型OfferVM不匹配。另一种方法是使用[UIHint("TemplateName")]属性标记要为EditorFor设置的媒体资源,其中TemplateName是您的编辑器模板的名称,在本例中为Offers

0

我有点晚了,但我会帮助。

您可以像这样包含模板名称。我用这个:

@ Html.EditorFor(型号=> model.DateOfBirth, “日期时间”,新{htmlAttributes = {新@class = “表单控制”}})

你在引号写模板。

在你的情况会是这样的

@ Html.EditorFor(型号=> model.RoomTypeName, “要约”)。

确保报价模板期待您发送EditorFor

希望这有助于