模型中的列表集合未发回控制器

时间:2012-09-24 20:21:05

标签: c# asp.net-mvc-3 razor

在过去的24小时内,我无能为力。我有一个模型,在其中填充一个集合对象,并在视图中rendred。所有渲染和回发都很好,当我将表单发回控制器时,列表TestExceptionDisplay为空/ null。请帮助,这让我发疯,我尝试了几种不同的方式,现在只是无能为力。

基本上在视图的末尾有一个foreach循环,我在其中渲染 TestExceptionDisplay属性,我确实为表单集合维护一个索引,以便在post上备份它,但无效但它始终为空。它渲染得很好,每个项目都正确显示正确的索引。

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

}


@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")';
    var runlogTestExceptionUrl = '@Url.Action("ListTestExceptions", "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="TestExceptionDisplay"></span></span>
        </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>

        <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)
                    {
                        foreach (var p in Model.TestExceptionDisplay)
                        {
                        <tr>
                            <td>
                                @Model.TestExceptionDisplay[index].Exception
                                @Html.HiddenFor(model => model.TestExceptionDisplay[index].Exception)
                            </td>
                        </tr>
                           index++;
                        }
                    }
                </table>
            </div>
        </div>



        @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)
@*  @Html.HiddenFor(model => model.RunLogEntryTestExceptionDisplay)*@


        <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>

1 个答案:

答案 0 :(得分:0)

我相信你最好让MVC进行实际的迭代并使用索引命名(根据需要),以便稍后绑定发布的元素。

为实现这一目标,我建议使用@Html.EditorFor(m => m.TestExceptionDisplay)创建编辑器模板。

类似于此处所做的:MVC 3 - Editor template for List<> Model

让你的编辑器模板只渲染一个项目(意思是,它的模型将是TestExceptionDisplay类型,而不是整个IEnumerable)。 MVC将为您处理迭代,并正确地为每个项目编号。