Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c# - List in EditorForMany comes up null on submit

I have a form with a EditorForMany Html Helper where I dynamically add pages (travelers). Each page has a number of fields that the user must fill out. This all saves and functions as designed. For each page I also want to have a dynamic attachment control. I've tried getting this attachment tool to work by adding another EditorForMany inside the existing EditorForMany. I've also tried a foreach loop and partial view that has worked in other situations. In both cases on submission of the form I get a null value for the attachments in each page object.

Do EditorForMany controls only work on one level? Am I unable to "nest" one foreach or EditorForMany inside another and get a returned value?

This is from the page cshtml:

<div id="travelerList" class="form-group">
    @Html.EditorForMany(r => r.TravelForms, r => r.Id.ToString(), true)
</div>

This is the attachment list inside of each TravelForm:

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>File Attachment</th>
                    <th>Created By</th>
                    <th>Created On</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (JEST.Models.TravelAttachment item in Model.TravelAttachments)
                {
                    @Html.Partial("_TravelAttachment", item)
                }
                @*@Html.EditorForMany(m => m.TravelAttachments, m => m.Id.ToString(), false)*@
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4"><button class="btn btn-link" id="travelbtn-add-file" travelFormId ="@Model.Id" >+ Add File</button></td>
                </tr>
            </tfoot>
        </table>

This is the travel attachment cshtml:

    <tr class="js-item-row">
@if (!Model.FileName.IsNullOrWhiteSpace())
{
    <td><a href="/" class="js-open-file" file-id="@Model.Id">@Model.FileName</a></td>
}
else
{
    @*<td><input class="form-control" type="file" name="File" value="@Model.File" /></td>*@
    @*<td>@Html.TextBoxFor(m => m.File, new { type = "file", @class = "form-control", name = "TravelForm.TravelAttachments[@Model.Id]" })</td>*@
    <td>@Html.TextBox("TravelAttachments["+Model.Id+"].File", Model.File, new { type = "file", @class = "form-control" }) </td>
}
@Html.HiddenFor(m => m.Id, new { name = "TravelForms[" + Model.TravelFormId + "].TravelAttachments[" + @Model.Id + "].Id" }) @Html.HiddenFor(m => m.TravelFormId, new { name = "TravelAttachments[" + @Model.Id + "].TravelFormId" }) @if (!Model.FileName.IsNullOrWhiteSpace()) { @Model.CreatedPerson.FullName }

Sorry folks, the format simply will not work for me here, but I hope that this is able to communicate my question clearly enough. Thank you

question from:https://stackoverflow.com/questions/65852700/list-in-editorformany-comes-up-null-on-submit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...