Repeating Partial Views MVC5 Form

I have an MVC form where I need to scroll through a set of database records and continue to display partial views containing data.

This is an input form. Meaning, if the database contains 50 records, each of which has a "FirstName", how do I handle this? There will be 50 input fields "FirstName".

I am using Entity Framework. I have not had to use this template in the past, and I'm not sure how it handles correctly in MVC5.

The parent class takes a List <> of these things, and then loops and displays partial views containing the form. BeginForm () is in the parent view. Particles are taken in special objects from the <> list for use as form fields.

My question relates to a handle to handle this on the server, which means the HttpPost method. Do I have a handle to the original List <> object that was passed to the parent view? Will it contain all updates automatically for every child contained inside?

+2
source share
2 answers

The answer, as Stephen Murke recommended, is to simply use the EditorTemplate for a custom object.

So now the parent view is simple:

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

Where m.HistoryDetail is a List <>. I just moved the part to the EditorTemplates subdirectory and renamed it according to the name of the object.

MVC , HTML, 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>
+1

, , :

@model string
<label for="name">First Name:</label>
<input type="text" readonly="readonly" id="name" value="@Model" />

, , , . foreach:

@foreach(var firstName in Model.FirstNames)
{
    @Html.PartialView("_NameOfYourPartialView", firstName)
}
0

All Articles