首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BeginCollectionItem与MVC核心的结合

BeginCollectionItem与MVC核心的结合
EN

Stack Overflow用户
提问于 2018-11-13 17:45:49
回答 1查看 1.6K关注 0票数 2

我似乎没有遇到与用MVC核心实现BeginCollectionItem时遇到的问题相同的问题,我没有收到任何数据。

我有一个主页,上面有大量的工作,但是这里是javascript,它为页面添加了新的部分,以及将在下面添加的部分。

代码语言: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>

现在,我们有这个部分开始我们正在建立的形式。

代码语言:javascript
复制
@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>

然后,我有一个部分,我们将加入越来越多的。

代码语言:javascript
复制
@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>
}

邮件的回复寄到这里..。

代码语言:javascript
复制
[HttpPost]
public async Task<IActionResult> PostQuizUpdate(IEnumerable<Sections> Sections)
{
    ...
}

就像我之前说过的,当我点击提交时,我没有得到文章中的章节列表。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-14 04:12:43

您的脚本将包含BeginCollectionItem的部分添加到<div id="sortable-survey" class="sortable-survey">元素中。但是该元素包含您的<form>....</form>元素,因此$("#sortable-survey").append(html);将在关闭的</form>标记之后追加</form>,因此,<form>本身将不包含任何表单控件,因此没有任何要提交的内容。

更改html,使<div>位于<form>元素中。

代码语言:javascript
复制
@using (Html.BeginForm("PostQuizUpdate", "Quiz", FormMethod.Post))
{
    <div id="sortable-survey" class="sortable-survey">
        ....
    </div>
}

这样,附加的表单控件就在表单标记中。

另外,如果Sections属性包含任何现有元素,那么await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", Model);将抛出一个The model item passed into the dictionary is of type .. but this dictionary requires a model item of type异常。您需要将foreach循环修改为

代码语言:javascript
复制
foreach (Sections section in Model.Quiz.Sections)
{
    await Html.RenderPartialAsync("~/Views/Admin/Quiz/_Section.cshtml", section); // section, not Model
}

此外,我建议您在将模型传递到视图之前在控制器中初始化集合,以便可以删除视图中的if null检查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53286776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档