0

我正在尝试将模型中的简单列表发布回控制器。它正确呈现,但在发布时它总是空的。我注意到当我使用 @Html.Hidden 的 id 和 name 是不同的例如 id 变成TestExceptionDisplay_[0]__Exception和 name 变成TestExceptionDisplay[0].Exception. 所以我使用一种输入类型,并尝试按照它喜欢的方式呈现,但它不起作用。我尝试了部分视图,编辑器模板,似乎没有任何效果。TestException Display 是一个非常简单的对象列表,称为 public List<RunLogEntryTestExceptionDisplay> TestExceptionDisplay { get; set; }

public class RunLogEntryTestExceptionDisplay
{
    public string Exception { get; set; }
}

视图中的片段使用 for 循环呈现列表,列表呈现正常,但在后 TestExceptionDisplay 始终为 NULL:

@if (Model.TestExceptionDisplay != null)
{
    for (var i = 0; i < Model.TestExceptionDisplay.Count; i++)
    {
    <tr>
        <td>
            @Model.TestExceptionDisplay[i].Exception
            @*@Html.HiddenFor(m => m.TestExceptionDisplay[i].Exception)*@
            <input  name="RunLogEntry.TestExceptionDisplay_[@i]__Exception" type="hidden" value="@Model.TestExceptionDisplay[i].Exception" />
        </td>
    </tr>
    }
}

或者这甚至不适用于发布时的编辑器模板

  @if (Model.TestExceptionDisplay != null)
                {

                    @Html.EditorFor(x => x.TestExceptionDisplay)

                }

Shared\EditorTemplates 中的编辑器模板,其确切名称为 RunLogEntryTestExceptionDisplay

@model RunLog.Domain.Entities.RunLogEntryTestExceptionDisplay
<div>
    @Html.EditorFor(x => x.Exception)
    @Html.HiddenFor(x => x.Exception)
</div>

呈现的 HTML 如下所示,我没有复制整个列表,请不要让 ID 和名称看起来以不同的命名约定呈现。如果我使用编辑器模板或 for 循环,这两种情况都会发生。谢谢

 <input id="TestExceptionDisplay_9__Exception" name="TestExceptionDisplay[9].Exception" type="hidden" value="&quot;1404 TestException - Parameters:Unable to calculate result, assay is not calibrated for Assay Number (135), Assay Version (5), Track (1), Lane (2), and Reagent Lot Number (26600LI06)&quot;" />

调试视图

完整视图:

   @model RunLog.Domain.Entities.RunLogEntry
    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_Layout.cshtml";

    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/errorCode.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/testexception.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/runLogEntry.js")" type="text/javascript"></script>
    <script type="text/javascript">
        var runlogListErrorsUrl = '@Url.Action("ListErrorCodes", "RunLogEntry")';


    </script>
    <fieldset>
        <legend>Enter a new Run Log Entry</legend>
        @using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.ValidationSummary(true)
            <div class="exception">@(ViewBag.ErrorMessage)</div>
            <div class="bodyContent">
                <span class="leftContent">Load List File (Select): </span><span class="rightContent">
                    <input type="file" name="file" id="file1" style="width: 500px" />
                </span>
            </div>
                               if (Model.LoadListStoredFileName != null)
                               {
            <div class="bodyContent">
                <span class="leftContent">Attached Load List: </span><span class="rightContent">
                    @Html.ActionLink(Model.LoadListFileName, "Download", new { @file = Model.LoadListStoredFileName })
                </span>
            </div>
                               }
            <div class="bodyContent">
                <span class="leftContent">Output File (Select): </span><span class="rightContent">
                    <input type="file" name="file" id="file2" style="width: 500px" />
                </span>
            </div>
                               if (Model.OutputStoredFileName != null)
                               {
            <div class="bodyContent">
                <span class="leftContent">Attached Output: </span><span class="rightContent">
                    @Html.ActionLink(Model.OutputFileName, "Download", new { @file = Model.OutputStoredFileName })
                </span>
            </div>
                               }
            <div class="bodyContent">
                <span class="leftContent">Import Files: </span>
                <button name="submit" class="art-button" type="submit" value="Upload" style="width: 100px">
                    Upload</button>
                <button name="submit" class="art-button" type="submit" value="Remove" style="width: 100px">
                    Remove</button>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Operator")
                </span><span class="rightContent">
                    @Html.DropDownList("OperatorID", String.Empty)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Run ID")
                </span><span class="rightContent">[Generated] </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Run Start Date / Time")
                </span><span class="rightContent">
                    @Html.EditorFor(model => model.RunDate)
                    &nbsp;
                    @Html.DropDownList("Hour", ListHelpers.HourList())
                    :
                    @Html.DropDownList("Minute", ListHelpers.Minute15List())
                    &nbsp;
                    @Html.DropDownList("AMPM", ListHelpers.AMPMList())
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("System")
                </span><span class="rightContent">
                    @Html.DropDownList("SystemID", String.Empty)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Run Type")
                </span><span class="rightContent">
                    @Html.DropDownList("RunTypeID", String.Empty)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Run Description")
                </span><span class="rightContent">
                    @Html.TextAreaFor(model => model.RunDescription, new { style = "width: 600px; height=30px" })
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Assay Performance Issues")
                </span><span class="rightContent">
                    @Html.DropDownList("AssayPerformanceIssues1", ListHelpers.YesNoList())
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Tests/Cycles Requested")
                </span><span class="rightContent">
                    @Html.EditorFor(model => model.SPTestsRequested)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Tests/Cycles Completed")
                </span><span class="rightContent">
                    @Html.EditorFor(model => model.SPTestsCompleted)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Run Status")
                </span><span class="rightContent">
                    @Html.DropDownList("RunStatusID", String.Empty)
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Assay")
                </span><span class="rightContent">
                    @Html.ListBoxFor(model => model.SelectedAssayIDs, new MultiSelectList(RunLog.Domain.Lists.GlobalList.AssayListItems(), "ID", "Name", Model.SelectedAssayIDs))
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Error Code")
                </span><span class="rightContent"><span id="ChildDialogLink" class="treeViewLink">Click
                    here to Select Error Codes</span>
                    <br />
                    <span id="ErrorCodeDisplay" style="cursor: pointer; text-decoration: underline;">@(Model.ErrorDescription)</span>
                    @Html.HiddenFor(model => model.ErrorDescription)
                </span>
            </div>

            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Test Exceptions")
                </span><span class="rightContent"><span id="TestExceptionChildDialogLink" class="treeViewLink">
                    Click here to View Test Exceptions</span>
                    <br />
                    <span id="TestExceptionDisplayy"></span></span>
            </div>
            <div id="testExceptiontreeview" title="Dialog Title" style="font-size: 10px; font-weight: normal;
                overflow: scroll; width: 800px; height: 450px;">
                <div id="testExceptions">
                </div>
                <div id="inputTestExceptions">
                    <table class="grid" style="width: 450px; margin: 3px 3px 3px 3px;">
                        <thead>
                            <tr>
                                <th>
                                    Exception String
                                </th>
                                <th>
                                    Comment
                                </th>
                            </tr>
                        </thead>
                        @* @{var index = 0;}*@
                        @if (Model.TestExceptionDisplay != null)
                        {
                            for (var i = 0; i < Model.TestExceptionDisplay.Count; i++)
                            {
                            <tr>
                                <td>
                                    @Model.TestExceptionDisplay[i].Exception
                                    @Html.HiddenFor(m => m.TestExceptionDisplay[i].Exception)
                                </td>
                            </tr>

                            }
                        }
                    </table>
                </div>
            </div>

            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Service Entry Request")
                </span><span class="rightContent">
                    @Html.DropDownList("ServiceRequest", ListHelpers.YesNoList())
                </span>
            </div>
            <div class="bodyContent">
                <span class="leftContent">
                    @Html.Label("Problem Description")
                </span><span class="rightContent">
                    @Html.TextArea("ProblemDescription", new { style = "width: 600px; height: 30px" })
                </span>
            </div>
            <p>
                <input id="LogType" type="hidden" value="Run" />
                <input id="ID" type="hidden" value="0" />
                @if (Model.ExitCode == "1")
                {
                    @Html.Hidden("ExitCode", Model.ExitCode)
                }
                else
                {
                    <input id="ExitCode" type="hidden" value='0' />
                }
            </p>

            @Html.HiddenFor(model => model.MaxReplicateId)
            @Html.HiddenFor(model => model.MinReplicateId)
            @Html.HiddenFor(model => model.OutputFileName)
            @Html.HiddenFor(model => model.OutputStoredFileName)
            @Html.HiddenFor(model => model.LoadListFileName)
            @Html.HiddenFor(model => model.LoadListStoredFileName)
            @Html.HiddenFor(model => model.MinTestCompletionDate)
            @Html.HiddenFor(model => model.MaxTestCompletionDate)

            <div class="bodyContent">
                <span class="leftContent"></span><span class="rightContent">
                    <button name="submit" class="art-button" type="submit" value="Create">
                        Create</button></span>
            </div>
        }
    </fieldset>
    <script src="@Url.Content("~/Scripts/exitCode.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/exitCode.js")" type="text/javascript"></script>
    <div id="treeview" title="Dialog Title" style="font-size: 10px; font-weight: normal;
        overflow: scroll; width: 800px; height: 450px;">
        <div id="errorCodes">
            @Html.RenderTree(CacheHelper.ErrorCodes(), ec => ec.Name, ec => ec.Children.ToList(), ec => (ec.ID).ToString(), null, "e")
        </div>
        <div id="inputReps" style="display: none;">
        </div>
    </div>
4

2 回答 2

1

你应该list在你的第一个方法中初始化你,然后传递给post方法。

于 2012-09-25T20:54:20.870 回答
1

为什么你期望你收到的模型中的列表被填充?

POST 上的模型仅填充来自表单的数据 - MVC 创建模型类的新实例并填充它可以匹配的字段。

正因为如此,确保您的列表被填充的常用方法是使用一个辅助方法来填充它们。然后,您可以从原始GetPost方法调用它来重新填充列表。

您唯一希望在回发时通过 PMV 在模型中填充列表的情况是,当表单元素中提供重建列表的详细信息时。

我可能在你的大量代码中错过了它,但我看不到你在任何地方这样做。

于 2012-09-25T20:59:06.890 回答