【问题标题】:How to retrieve IEnumerable List within model rendered for view model back to controller upon post如何在为视图模型渲染的模型中检索 IEnumerable 列表返回到控制器
【发布时间】:2012-09-20 21:36:08
【问题描述】:

我有一个文本文件,当用户上传文件时,控制器操作方法使用状态机解析该文件并使用通用列表来存储一些值。我以 IEnumerable 的形式将其传递回视图。在我的主视图中,基于这个可枚举的列表,我渲染了一个部分视图来迭代项目并显示标签和一个文本区域。用户可以在文本区域中添加他们的输入。当用户点击保存按钮时,渲染的局部视图中的这个可枚举列表为空。所以请提出任何解决方案。

这是我的主要观点

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

}

   @using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {

<div id="inputTestExceptions" style="display: none;">
        <table class="grid" style="width: 450px; margin: 3px 3px 3px 3px;">
            <thead>
                <tr>
                    <th>
                        Exception String
                    </th>
                    <th>
                        Comment
                    </th>
                </tr>             </thead>

            <tbody>
                @if (Model.TestExceptions != null)
                {
                    foreach (var p in Model.TestExceptions)
                    {
                        Html.RenderPartial("RunLogTestExceptionSummary", p);

                    }
                }
            </tbody>
        </table>

    </div>
     }

部分视图如下:

@model RunLog.Domain.Entities.RunLogEntryTestExceptionDisplay
<tr>
    <td>
    @Model.TestException@
       </td>
        <td>@Html.TextAreaFor(Model.Comment, new { style = "width: 200px; height: 80px;" })
    </td>
</tr>

控制器动作

[HttpPost]
    public ActionResult Create(RunLogEntry runLogEntry, String ServiceRequest, string Hour, string Minute, string AMPM,
                                 string submit, IEnumerable<HttpPostedFileBase> file, String AssayPerformanceIssues1, IEnumerable<RunLogEntryTestExceptionDisplay> models)
    {

}

问题是包含异常字符串的测试异常,并且注释返回 null。

更新

public class RunLogEntry
{
   SOME OTHER FIELDS

    [NotMapped]
    public IEnumerable<RunLogEntryTestExceptionDisplay> TestExceptions { get; set; }
}



public class RunLogEntryTestExceptionDisplay
{
    public string TestException { get; set; }
    public string Comment { get; set; }
}



@using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
         if (Model.TestExceptions != null)
           {
               if (Model.TestExceptions.Count() > 0)
               {
        <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>
                @Html.HiddenFor(model => model.TestExceptions)
                @*<input id="ExceptionString" type="hidden" value="@Model.ExceptionString" />*@
            </span>
        </div>
               }
           }


   <div id="inputTestExceptions" style="display: none;">
        <table class="grid" style="width: 450px; margin: 3px 3px 3px 3px;">
            <thead>
                <tr>
                    <th>
                        Exception String
                    </th>
                    <th>
                        Comment
                    </th>
                </tr>
            </thead>
            @if (Model.TestExceptions != null)
            {
                var index = 0;
                foreach (var p in Model.TestExceptions)
                {
                <tr>
                    <td>@p.TestException
                        <input type="hidden" name="RunLogEntry.TestExceptions[@index].ExceptionString" value="@p.TestException" />
                    </td>
                    <td>
                        <textarea name="RunLogEntry.TestExceptions[@index].Comment" style ="width: 200px; height: 80px;">@p.Comment</textarea>
                        <input type="hidden" name="RunLogEntry.TestExceptions[@index].Comment" value="@p.Comment" />
                    </td>
                    @* Html.RenderPartial("RunLogTestExceptionSummary", p);*@
                </tr>
                                                                                                 index++;
                }

            }
        </table>
    </div>
}

【问题讨论】:

    标签: jquery asp.net-mvc razor


    【解决方案1】:

    要发布集合,您需要以数组样式命名表单元素。您还必须保留一个计数器,以便您可以在 html 元素名称/id 中设置数组索引。

    @{var index = 0;}
    @foreach (var p in Model.TestExceptions) {
      <tr>
        <td>
          @Model.TestException@
         </td>
          <td>
              <textarea name="@Html.FieldNameFor(m => m.TestExceptions[@index].Comment" id="@Html.FieldIdFor(m => m.TestExceptions[@index].Comment" style ="width: 200px; height: 80px;">@p.Comment</textarea>
              <input type="hidden" name="@Html.FieldNameFor(m=> m.TestExceptions[@index].ExceptionString" id="@Html.FieldIdFor(m=> m.TestExceptions[@index].ExceptionString" value="@p.ExceptionString" />
          </td>
      </tr>
    index++;
    }
    

    我不知道你的模型是什么样子的,但是你提到了一个异常字符串,所以我使用了一个隐藏的表单元素来存储“ExceptionString”属性的值,以便模型绑定可以在你获取这些值时邮政。

    如果您的 RunLogEntry 类除了 TestExceptions 集合之外还有其他属性,您可以通过包含将映射到 RunLogEntry 属性的表单元素来确保它在 POST 上正确绑定。例如,如果你有 RunLogEntry.Foo 属性,你可以把它放在上述循环之外的某个地方:

    <input type="hidden" name="RunLogEntry.Foo" value="@Model.Foo" />
    

    【讨论】:

    • 这是模型 RunLogEntry 中的一个属性 public IEnumerable TestExceptions { get;放; }
    • 然后有这个@model RunLog.Domain.Entities.RunLogEntryTestExceptionDisplay 它有两个属性,即测试异常字符串和注释字符串
    • 在主视图中,我将测试异常显示为循环中的一部分,它渲染得很好,只是在发布后我看不到值
    • 我摆脱了部分视图并按照您上面提到的进行渲染,testexception 仍然返回为 0 计数,所以我无法获得个别测试异常和评论。
    • 你能粘贴 RunLogEntry 类的代码,以及它包含的任何类(包括 RunLogEntryTestExceptionDisplay )吗?
    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多