具有重复部分视图的MVC5表单 [英] MVC5 Form with repeating partial views

查看:79
本文介绍了具有重复部分视图的MVC5表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC表单,其中我必须遍历数据库记录集并继续显示包含数据的部分视图.

I have an MVC form where I have to loop through a database recordset and continue to display partial views that contain the data.

这是一个输入表格.这意味着如果数据库包含50条记录,每条记录都带有"FirstName",该如何处理?将有50个名字"输入字段.

This is an input form. Meaning if the database contains 50 records, each with "FirstName", how do I handle this? There are going to be 50 "FirstName" input fields.

我正在使用实体框架.过去我不必使用此模式,也不确定在MVC5下如何正确处理此模式.

I am using the Entity Framework. I have not had to use this pattern in the past and am not sure how this is properly handled under MVC5.

父类接受这些内容的列表<>,然后循环并显示包含表单的部分视图. BeginForm()在父视图中.局部对象从List<>中获取单个对象,以用作表单字段.

The parent class takes in a List<> of these things then loops and displays the partial views that contain the form. The BeginForm() is in the parent view. The partials take in the singular objects from the List<> to use as the form fields.

我的问题与在服务器上处理该句柄有关,即HttpPost方法中的句柄.我是否可以处理传递给父视图的原始List<>对象?会自动包含其中包含的每个子对象的所有更新吗?

My question relates to handle to handle this on the server, meaning in the HttpPost method. Do I have that handle the original List<> object that was passed to the parent view? Will that contain all of the updates automatically to each child object contained within?

推荐答案

斯蒂芬·默克(Stephen Murke)提出的答案只是为自定义对象使用EditorTemplate.

The answer, as recommended by Stephen Murke, is just to use an EditorTemplate for the custom object.

现在,父视图仅具有:

@Html.EditorFor(m => m.HistoryDetail)

其中m.HistoryDe​​tail是列表<>.我只是将部分对象移到了EditorTemplates子文件夹中,并将其重命名为与对象名称匹配.

Where m.HistoryDetail is a List<>. I simply moved the partial in an EditorTemplates sub-folder and renamed it to match the object name.

MVC框架负责其余的工作,我得到的HTML列表中的每个项目都有唯一的ID和名称.

The MVC framework takes care of the rest and I get resulting HTML that has unique IDs and names for each item in the List<>:

<textarea class="form-control" cols="25" id="HistoryDetail_2__ReferralComments" name="HistoryDetail[2].ReferralComments" rows="5" style="max-width: 100%; width: 100 %;">
    Comments for the Approval Level
</textarea>
<span class="field-validation-valid" data-valmsg-for="HistoryDetail[2].ReferralComments" data-valmsg-replace="true"></span>

这篇关于具有重复部分视图的MVC5表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆