简体   繁体   English

在 MVC 核心中使用 BeginCollectionItem

[英]Using BeginCollectionItem with MVC Core

I don't appear to be having the same issues as most with the implementation of BeginCollectionItem with MVC core, I'm not receiving any of the data.在使用 MVC 核心实现 BeginCollectionItem 时,我似乎没有遇到与大多数相同的问题,我没有收到任何数据。

I have this main page that has alot of work on it but here is the javascript that adds new sections to the page and the partial that will be added below.我有这个主页,上面有很多工作,但这里是向页面添加新部分的 javascript,以及将在下面添加的部分。

@model QuizBuilderModel
...
function addSection() {
    $.ajax({
        url: '@Url.Action("AddSection","Quiz")',
        cache: false,
        success: function (html) {
        $("#sortable-survey").append(html);
        }
    });
}
...

<div class="quiz-builder" id="quiz-builder" style="@((Model.Quiz == null || string.IsNullOrEmpty(Model.Quiz.Id))? "display: none;" : "")">
        @{await Html.RenderPartialAsync("~/Views/Admin/Quiz/_QuizBuilder.cshtml", Model);}
</div>

Now, we have this partial to start the form we're building.现在,我们有这个部分来启动我们正在构建的表单。

@model QuizBuilderModel

<div id="sortable-survey" class="sortable-survey">
    @using (Html.BeginForm("PostQuizUpdate", "Quiz", FormMethod.Post))
    {
        @if (Model.Quiz != null && !string.IsNullOrEmpty(Model.Quiz.Id))
        {
            <input type="submit" class="btn btn-lg btn-outline-primary top-right-button top-right-button-single" id="SaveQuizModal" name="SaveQuizModal" value="Save Quiz" />
        }

        @if (Model.Quiz != null && Model.Quiz.Sections != null)
        {
            foreach (Sections section in Model.Quiz.Sections)
            {
                await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", Model);
            }
        }
    }
</div>

<div>
    <div class="text-center">
        <button type="button" class="btn btn-outline-primary btn-sm my-5" id="AddSection" onclick="addSection();">
            <i class="simple-icon-plus btn-group-icon"></i>
            Add Section
        </button>
    </div>
</div>

Then I have this partial section where we'll be adding more and more to it.然后我有这个部分部分,我们将在其中添加越来越多的内容。

@using HtmlHelpers.BeginCollectionItemCore
@model Sections

@using (Html.BeginCollectionItem("Sections"))
{
    <div class="form-group">
        @Html.LabelFor(x => x.Title);
        @Html.EditorFor(m => m.Title, new { @class = "form-control" })
    </div>

    <div class="form-group">
        <label>SubTitle (optional)</label>
        @Html.EditorFor(m => m.SubTitle, new { @class = "form-control" })
    </div>

    <div class="form-group">
        <label>Instructions (optional)</label>
        <textarea type="text" class="form-control" placeholder="" id="Instructions" name="Instructions" required rows="10" cols="50"></textarea>
    </div>
}

The response of the post is sent here...帖子的回复发送到这里......

[HttpPost]
public async Task<IActionResult> PostQuizUpdate(IEnumerable<Sections> Sections)
{
    ...
}

Like i said before when I hit the submit I'm not getting the list of Sections in the post.就像我之前说的,当我点击提交时,我没有得到帖子中的部分列表。

Your script is appending the partial containing BeginCollectionItem to the <div id="sortable-survey" class="sortable-survey"> element.您的脚本将包含BeginCollectionItem的部分附加到<div id="sortable-survey" class="sortable-survey">元素。 But that element contains your <form>....</form> element, so $("#sortable-survey").append(html);但是该元素包含您的<form>....</form>元素,因此$("#sortable-survey").append(html); will append the html after the closing </form> tag, and as a result, your <form> itself will not contain any form controls, so there is nothing to submit.将在结束</form>标记后附加 html,因此,您的<form>本身将不包含任何表单控件,因此无需提交任何内容。

Alter the html so that the <div> is within the <form> element更改 html,使<div>位于<form>元素内

@using (Html.BeginForm("PostQuizUpdate", "Quiz", FormMethod.Post))
{
    <div id="sortable-survey" class="sortable-survey">
        ....
    </div>
}

so that the appended form controls are within the form tags.以便附加的表单控件位于表单标签内。

As a side note, if your Sections property contains any existing elements, then your await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", Model);作为旁注,如果您的Sections属性包含任何现有元素,那么您的await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", Model); will throw a The model item passed into the dictionary is of type .. but this dictionary requires a model item of type exception.将抛出一个传入字典的模型项的类型为 .. 但该字典需要一个异常类型的模型项 You need to modify the foreach loop to您需要将foreach循环修改为

foreach (Sections section in Model.Quiz.Sections)
{
    await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", section); // section, not Model
}

In addition, I recommend you initialize your collections in the controller before passing the model to the view so that you can delete the if null checks in the view.此外,我建议您在将模型传递给视图之前在控制器中初始化您的集合,以便您可以删除视图中的if null检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM