2013-07-03 49 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

希望這有助於