2010-08-22 62 views
2

我有一個模型,其中包含我想使用EditorFor的QuestionEditModel的列表。在viewmodel中定義子集合的EditorTemplate

通常情況下,我只需在集合上調用EditorFor,MVC將完成剩下的工作。但是,我需要單獨的QuestionEditModel根據對象內某個字段的值使用不同的EditorTemplates。

我會一直認爲,這樣做的方法是像

<%: Html.EditorFor(model=>model.Questions), [fieldname from individual question] %> 

,但我無法弄清楚如何告訴它看看現在選擇的問題,並從使用EntryType場該問題來確定使用哪個EditorTemplate。

所以,我想這個

  <% foreach (Reviewer.Models.QuestionEditModel qem in Model.Questions) 
      { 
       Html.EditorFor(q=>qem, qem.EntryType, null); 
      } %> 

但這並不到頁面呈現任何內容。奇怪的是,如果我設置了一個斷點並運行了代碼,這確實會調用正確的EditorTemplate,正確的模型數據被傳入並且沒有例外,但它不會呈現任何內容。

在這種情況下,我需要做一些額外的工作來將渲染的EditorTemplate重新放回到我的頁面嗎?

編輯:編輯視圖的

的完整代碼。

<% using (Html.BeginForm()) {%> 
    <%: Html.ValidationSummary(true) %> 

    <%: Html.HiddenFor(model=>model.AcadPeriod) %> 
    <%: Html.HiddenFor(model=>model.ReviewID) %> 
    <%: Html.HiddenFor(model=>model.ReviewName) %> 
    <%: Html.HiddenFor(model=>model.CategoryID) %> 
    <%: Html.HiddenFor(model=>model.CategoryName) %> 
    <%-- Categories not getting returned in model for some reason. Use EditorFor or DisplayFor instead of loop? --%> 
    <%: Html.HiddenFor(model=>model.Categories) %> 
    <%: Html.HiddenFor(model=>model.ClassificationID) %> 
    <%: Html.HiddenFor(model=>model.ClassificationName) %> 

    <div style="width:100%"> 

    <div style="float:left"> 
     <ul style="list-style-type:none"> 
      <% for (int i = 0; i < Model.Categories.Count(); i++) 
       { %> 
      <li style="background-color:Gray; border: 1px solid black; padding: 3px 3px 3px 3px; margin-bottom: 2px"> 
      <%: Html.ActionLink(Model.Categories[i].name, "Edit", new { AcadPeriod = Model.AcadPeriod, ClassificationID=Model.ClassificationID, ReviewID=Model.ReviewID, CategoryID=Model.Categories[i].category_id })%> 
      </li> 
      <% }%> 
     </ul> 
    </div> 

    </div> 
       <% foreach (Reviewer.Models.QuestionEditModel qem in Model.Questions) { %> 
        <%: Html.EditorFor(q=>qem, qem.EntryType,null); %> 
       <% } %> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 

<% } %> 

編輯2:

完整視圖,控制器和模板代碼的要求。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Reviewer.Models.ReviewEditModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Edit 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h1><%: Model.AcadPeriod %> &gt; <%: Model.ClassificationName %> &gt; <%: Model.ReviewName %></h1> 

<% using (Html.BeginForm()) {%> 
    <%: Html.ValidationSummary(true) %> 

    <%: Html.HiddenFor(model=>model.AcadPeriod) %> 
    <%: Html.HiddenFor(model=>model.ReviewID) %> 
    <%: Html.HiddenFor(model=>model.ReviewName) %> 
    <%: Html.HiddenFor(model=>model.CategoryID) %> 
    <%: Html.HiddenFor(model=>model.CategoryName) %> 
    <%-- Categories not getting returned in model for some reason. Use EditorFor or DisplayFor instead of loop? --%> 
    <%: Html.HiddenFor(model=>model.Categories) %> 
    <%: Html.HiddenFor(model=>model.Questions) %> 
    <%: Html.HiddenFor(model=>model.ClassificationID) %> 
    <%: Html.HiddenFor(model=>model.ClassificationName) %> 

    <div style="width:100%"> 

    <div style="float:left;width: 15%"> 
     <ul style="list-style-type:none"> 
      <% for (int i = 0; i < Model.Categories.Count(); i++) 
       { %> 
      <li style="background-color:Gray; border: 1px solid black; padding: 3px 3px 3px 3px; margin-bottom: 2px"> 
      <%: Html.ActionLink(Model.Categories[i].name, "Edit", new { AcadPeriod = Model.AcadPeriod, ClassificationID=Model.ClassificationID, ReviewID=Model.ReviewID, CategoryID=Model.Categories[i].category_id })%> 
      </li> 
      <% }%> 
     </ul> 
    </div> 

    <div style="float:left; width: 80%; margin-left: 5px"> 

<% foreach (Reviewer.Models.QuestionEditModel qem in Model.Questions) { %> 
<%: Html.EditorFor(q=>qem, qem.EntryType,null) %> 
<% } %> 

    </div> 
    </div> 
    <div style="clear:both" /> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 

<% } %> 

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

編輯模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Reviewer.Models.QuestionEditModel>" %> 

<div style="width:100%; border: 1px solid black"> 
<div style="width: 100%; border: 1px solid black"><h2><%: Model.QuestionName %></h2></div> 
<div style="width:25%; display:inline; border: 1px solid black; float:left"> 
    <%: Model.QuestionText %> 
</div> 
<div style="width:70%; border: 1px solid black; float:left"> 
    <%: Html.TextAreaFor(model=>model.Answer) %> 
    <%:Html.ValidationMessageFor(model=>model.Answer) %> 
</div> 

<div style="clear:both" /> 
</div> 

    <fieldset> 
     <legend>TEXT</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.QuestionID) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.QuestionID) %> 
      <%: Html.ValidationMessageFor(model => model.QuestionID) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.QuestionName) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.QuestionName) %> 
      <%: Html.ValidationMessageFor(model => model.QuestionName) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.QuestionText) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.QuestionText) %> 
      <%: Html.ValidationMessageFor(model => model.QuestionText) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.DefaultText) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.DefaultText) %> 
      <%: Html.ValidationMessageFor(model => model.DefaultText) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.EntryType) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.EntryType) %> 
      <%: Html.ValidationMessageFor(model => model.EntryType) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.HelpText) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.HelpText) %> 
      <%: Html.ValidationMessageFor(model => model.HelpText) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Answer) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.Answer) %> 
      <%: Html.ValidationMessageFor(model => model.Answer) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.OptionValue) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.OptionValue) %> 
      <%: Html.ValidationMessageFor(model => model.OptionValue) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.completedBy) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.completedBy) %> 
      <%: Html.ValidationMessageFor(model => model.completedBy) %> 
     </div> 

     Option Required: <%:Html.TextBoxFor(model=>model.OptionRequired) %> 
     Answer Required: <%: Html.TextBoxFor(model=>Model.AnswerRequired) %> 

    </fieldset> 

編輯(GET)操作:

 public ActionResult Edit(string AcadPeriod, string ClassificationID, string ReviewID, int CategoryID) 
    { 
     Reviewer.Models.ReviewEditModel dset1 = rr.GetReviewEditModel(AcadPeriod, ReviewID, CategoryID.ToString(), ClassificationID); 
     return View(dset1); 
    } 

編輯(POST)行動:

 [HttpPost] 
    public ActionResult Edit(Reviewer.Models.ReviewEditModel model) 
    { 
     try 
     { 
      foreach (Reviewer.Models.QuestionEditModel qem in model.Questions) 
      { 
       if (qem.Answer == null || qem.OptionValue == null) { qem.completedBy = this.HttpContext.User.Identity.Name; } 
      } 

      if (ModelState.IsValid) 
      { 
       rr.SaveReviewEditModel(model); 

       return RedirectToAction("Index"); 
      } 
      else { return View(model); } 
      } 
     catch 
     { 
      return View(model); 
     } 
    } 

回答

1

你必須告訴它實際呈現(<%: %>)什麼:

<% foreach (Reviewer.Models.QuestionEditModel qem in Model.Questions) { %> 
    <%: Html.EditorFor(q=>qem, qem.EntryType, null) %> 
<% } %> 
+0

真是個傻瓜,我以爲我會很聰明,並試圖把它們都放在一個代碼塊,完全忘記了: 。但是,當運行你的代碼時,我得到了一個CS1026:),但是我看不到任何缺少的偏差。 我已將完整的「編輯視圖」代碼添加到原始文章中。 – hermiod 2010-08-22 22:13:33

+0

錯誤出現在Html.EditorFor行上。 – hermiod 2010-08-22 22:20:09

+0

這只是;在線的末尾扔掉它。 完美地完成了這項工作,謝謝Necros。不敢相信我離得太近了! :) – hermiod 2010-08-22 22:21:38

相關問題