【问题标题】:Get list data on view in controller获取控制器视图中的列表数据
【发布时间】:2016-03-03 05:40:29
【问题描述】:

我有一个视图,我在其中循环渲染了部分视图。有一个列表,部分视图与列表中的每个项目绑定。输入值后,我没有在控制器上获取列表的值。

这是我的看法:

<table id="resourceRequirement" class="table" width="100%" border="0">
    <thead>
        <tr style="background-color:#dfdfdf;">
            <td><div align="center">PRIORITY</div></td>
            <td><div align="center">SYSTEM RESOURCE / COMPONENT</div></td>
            <td><div align="center">RECOVERY TIME OBJECTIVE</div></td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ResourceRequirement)
        {
            @Html.Partial("~/Views/Shared/_ResourceRequirement.cshtml", item)
        }
    </tbody>
</table>

这是我的部分观点:

@model DisasterManagementSystem.Models.BusinessImpactAnalysis.ResourceRequirement
<tr>
    <td>
        @Html.TextBoxFor(m => m.priority)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.priority)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.systemresource)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.systemresource)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.receveryTime)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.receveryTime)
        </div>
    </td>
</tr>

这是我的清单:

public List<ResourceRequirement> ResourceRequirement { get; set; }

课程在这里:

public class ResourceRequirement
{
    [Required(ErrorMessage = "*")]
    public string priority { get; set; }

    [Required(ErrorMessage = "*")]
    public string systemresource { get; set; }

    [Required(ErrorMessage = "*")]
    public string receveryTime { get; set; }
}

当我尝试从帖子中的模型获取列表时,请告知我将列表设为空。

【问题讨论】:

  • 也许渲染这个视图的控制器动作没有在模型中提供任何值?
  • 在填写详细信息后最初呈现空白表单,它不返回值。只返回 null
  • 你也可以显示控制器代码吗?

标签: .net asp.net-mvc-5


【解决方案1】:

您使用了foreach 循环,并且部分生成了没有索引器的重复name 属性(因此无法绑定到集合)和重复id 属性(无效的html)。

使用EditorTemplate 代替部分视图。将当前的局部视图重命名为ResourceRequirement.cshtml(即与类的名称匹配)并将其放在/Views/Shared/EditorTemplates 文件夹(或/Views/yourController/EditorTemplates 文件夹)中

然后在主视图中,删除foreach循环并替换为

<tbody>
    @Html.EditorFor(m => m.ResourceRequirement)
</tbody>

EditorFor() 方法接受IEnumerable&lt;T&gt; 并为您的集合中的每个项目生成正确的 html。如果您检查 html,您现在将在表单控件中看到正确的名称属性

<input type="text" name="ResourceRequirement[0].priority" .... />
<input type="text" name="ResourceRequirement[1].priority" .... />
<input type="text" name="ResourceRequirement[2].priority" .... />

等等。当您提交表单时,它将绑定到您的模型(将其与您当前生成的进行比较)

【讨论】:

  • 编辑器模板的文件夹路径应该是什么。我没有理解您对“/Views/Shared/EditorTemplates 文件夹(在 /Views/yourController/EditorTemplates 文件夹中)”行的含义
  • 你能指导如何使用jquery在上表中添加一行吗?这样我们就得到了数据
  • 假设您的控制器名为HomeController,那么它可以位于Views/Shared/EditorTemplates/ 文件夹或Views/Home/EditorTempates/ 文件夹中。视图引擎首先在控制器特定文件夹中搜索EditorTemplates,如果没有找到,则在Shared 文件夹中搜索。这使您可以为每个班级拥有多个EditorTemplates。在您的情况下,只需将其重命名并将其移动到 /Views/Shared/EditorTemplates 文件夹
  • 至于使用 jquery 添加新行,这是一个完全不同的问题。首先研究答案hereherehere
【解决方案2】:

因为您希望 List 在 View 中传递,只是在 Controller 中通过类似的方法传递 List

public Actionresult List()
{
  var search = from m in db.resourcerequirement select m;
  return PartialView("_List",search.tolist());
}

之后在部分视图_List中

@model DisasterManagementSystem.Models.BusinessImpactAnalysis.ResourceRequirement
<tr>
    <td>
        @Html.TextBoxFor(m => m.priority)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.priority)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.systemresource)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.systemresource)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.receveryTime)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.receveryTime)
        </div>
    </td>
</tr>

显示局部视图

@{Html.RenderAction("List", "ControllerName");}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多