我有一个模型,其中包含一个质疑列表,我想使用该编辑器。

通常,我只会在收藏中致电Editorfor,其余的MVC将完成。但是,我需要单个质疑模型来根据对象内字段的值使用不同的编辑置换术。

我会认为这样做的方法将是类似的

<%: 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,正确的模型数据将传递到其中,没有例外,但是它不会呈现任何内容。

在这种情况下,我还需要做一些其他工作,以将渲染的EditeTemplate返回到我的页面上?

编辑:

编辑视图的完整代码。

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

编辑(邮政)操作:

        [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);
        }
    }
有帮助吗?

解决方案

您必须告诉它实际渲染的内容(<%: %>):

<% foreach (Reviewer.Models.QuestionEditModel qem in Model.Questions) { %>
     <%: Html.EditorFor(q=>qem, qem.EntryType, null) %>
<% } %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top