2011-08-18 69 views
0

我正在将MVC2应用程序转换为MVC3。 我似乎无法联合使用编辑器模板的视图。 所以在我看来代码是;无法获取在MVC3中工作的编辑器模板

@model IEnumerable<ITOF.Models.LatestContractNumber> 

@{ 
    ViewBag.Title = "EditContractNumbers"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

using (Html.BeginForm()) 
     { 
    <fieldset> 
     <legend>Edit Latest Contract Numbers</legend> 
     <div style="width:90%"> 
      <div style="width:45%; float:left; vertical-align:top;"> 
       <p>A contract number is in a format: AA/B999</p> 
       <p>Where AA is a 2 character code for the division, for example CD.</p> 
       <p>Where B is a letter for the contract, usually the first letter of the contract title.</p> 
       <p>999 is a 3 digit number.</p> 
       <p>The 999 number depends on the B letter. </p> 
       <p>Initially all the numbers start as 1 and are incremented each time the B letter is used.</p> 
       <p>This pages sets the initial 999 number for each B letter.</p> 
       <p>Once the application is up and running, this page should be used initially and then no more.</p> 
       @if (TempData["Message"] != null && TempData["Message"].ToString().Trim().Length > 0) 
        { 
        <p class="success">@TempData["Message"].ToString()</p> 
        }   
      </div> 
      <div style="width:45%; float:right; vertical-align:top;"> 
       <div> 
       <table> 
        <tr> 
         <td>Letter</td> 
         <td>Number</td> 
        </tr> 
        @Html.EditorForModel(Model) 
       </table> 
       </div> 
       <div style="padding-top:20px"> 
        <input type="submit" value="Submit Changes" /> 
       </div> 
      </div> 
     </div> 
    </fieldset> 
    } 

而我的编辑器模板是在正确的文件夹中,现在看起来像;

@model ITOF.Models.LatestContractNumber 
<tr> 
    <td> 
     @Html.HiddenFor(model => model.LatestContractNumberId) 
     @Html.DisplayFor(model => model.Letter) 
    </td> 

    <td> 
     @Html.TextBoxFor(model => model.Number, new { style = "width:30px" }) 
    </td> 
</tr> 

@ Html.EditorForModel(Model)中的模型与页面顶部的@model IEnumerable相关。 在编辑模板,这应该转化为ITOF.Models.LatestContractNumber的循环,因此,在我已经把@model ITOF.Models.LatestContractNumber

+0

当你说你的编辑器模板是在正确的文件夹中,它在哪个文件夹中? –

+0

编辑器模板文件夹又位于与视图相同的文件夹中。 – arame3333

+1

和EditorTemplate文件的名称是什么? –

回答

-1

这个问题原来是一个红鲱鱼。真正的问题是,已经迁移到MVC并开始用_作为前缀部分视图,编辑器模板的名称是无效的。 感谢拉斯凸轮问正确的问题,让我的答案。

0

首先模板的顶部,您正在使用的模型调用EditorForModel键入IEnumerable<ITOF.Models.LatestContractNumber>(这是您的视图的模型),但您应该用ITOF.Models.LatestContractNumber(这是您的模板期望看到/得到的模型)调用它。你需要一个foreach某处有

foreach(ITOF.Models.LatestContractNumber number in Model) { 
    @Html.EditorForModel(number) 
} 

其次,不是100%肯定,但EditorTemplates应该是共享文件夹中查看文件夹下。所以~/Views/Shared/EditorTemplates/,应该命名为类名LatestContractNumber.cshtml

HTH

+0

我想这是全错,对不起!使用Editor Templates的一个要点是你不必使用for循环。编辑模板文件夹可以与共享文件夹中的视图或文件夹位于同一文件夹中。如果模板将被不同文件夹中的视图使用,或者您认为将来可能会使用该模板,则使用共享文件夹。 – arame3333

相关问题