简体   繁体   English

为什么列表没有传递给 IActionResult

[英]Why list is not getting passed to IActionResult

Why is the list newsletterSubComponents not getting passed to IActionResult .为什么列表newsletterSubComponents没有传递给IActionResult Can anyone help please.任何人都可以请帮忙。 I know there is one option BindProperty but i can not use it since this list is coming from a partial view.我知道有一个选项BindProperty但我无法使用它,因为此列表来自局部视图。 I can only modify the list which is inside the model which is being passed to the partial_view.我只能修改传递给partial_view 的模型中的列表。

List in model型号列表

List<NewsletterSubComponents> newsletterSubComponents = {get; set;}

Partial view局部视图

  @{

    Model.newsletterSubComponents.Add(new Models.NewsletterSubComponents() { NewPlantArticleId = 0 });
    Model.newsletterSubComponents.Add(new Models.NewsletterSubComponents() { NewPlantArticleId = 2 });

}

@for (var i = 0; i < Model.newsletterSubComponents.Count; ++i)
{
    <select class="form-control Shoplist News-plant-select_1 mt-2" data-search="true" asp-for="@Model.newsletterSubComponents[i].NewPlantArticleId">
        <option value="0">Bitte auswählen</option>
    </select>
}

Controller控制器

 public IActionResult OnPostUpdateComponent(NewsletterComponents component, int? newsId, int? shortArticleId,
        List<NewsletterSubComponents> newsletterSubComponents, int? newPlant1, int? newPlant2, int? newPlant3)
    {
        var dbComponent = _context.NewsletterComponents.Find(component.NewsletterComponentId);
        if (dbComponent == null)
        {
            return RedirectToPage("./Index");
        }
        dbComponent.NewsId = newsId;
        dbComponent.ShortArticleId = shortArticleId;
        dbComponent.newsletterSubComponents = component.newsletterSubComponents;
        dbComponent.NewsletterMoreLink = component.NewsletterMoreLink;
        dbComponent.CustomLinkText = component.CustomLinkText;
        dbComponent.NewsleterComponentText = component.NewsleterComponentText;
        dbComponent.NewsletterComponentHeadline = component.NewsletterComponentHeadline;
        dbComponent.EditedBy = User.Identity.Name;
        dbComponent.EditedDate = DateTime.Now;
        _context.Update(dbComponent);
        _context.SaveChanges();
        UpdateNewsletterHtml((int)dbComponent.BelongsToNewsletterId);

        return RedirectToPage(new { id = dbComponent.BelongsToNewsletterId });
    }

newsletterSubComponents parameter is empty when i debug!调试时newsletterSubComponents参数为空!

I have update the method to pass the list via javascript, you can refer to the following example.我已经更新了通过javascript传递列表的方法,您可以参考以下示例。

In partial view, I inject the pagemodel and smimulate some data.在局部视图中,我注入了页面模型并模拟了一些数据。

@model projectname.Pages.IndexModel
@{

Model.newsletterSubComponents.Add(new Models.NewsletterSubComponents() { NewPlantArticleId = 0 });
Model.newsletterSubComponents.Add(new Models.NewsletterSubComponents() { NewPlantArticleId = 2 });

}

<form>
    @for (var i = 0; i < Model.newsletterSubComponents.Count; ++i)
    {
         <select class="form-control Shoplist News-plant-select_1 mt-2" data-search="true" onchange="changeVal(event)" asp-for="@Model.newsletterSubComponents[i].NewPlantArticleId">
            <option value="0">Bitte auswählen</option>
        </select>
    }
    @Html.AntiForgeryToken()
    <button type="button" onclick="send()">btn</button>
</form>

Use javascript to pass this list.使用 javascript 传递此列表。

@section Scripts{
<script>
    var SubComponents = [];
    var s = '@Model.newsletterSubComponents.Count()'
    for (var i = 0; i < s; i++) {
        SubComponents[i] = { "NewPlantArticleId": $("#newsletterSubComponents_" + i + "__NewPlantArticleId").val() }
    }
     function changeVal(e) {
        let nid = e.target.id
        let k = nid.replace(/[^0-9]/ig,"")
        SubComponents[k]={ "NewPlantArticleId": $("#newsletterSubComponents_" + k + "__NewPlantArticleId").val() }
    }
    function send() {
        $.ajax({
            url: '/?handler=UpdateComponent',
            method: 'post',
            headers: {
                RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
            },
            data: {
                newsletterSubComponents: SubComponents
            },
            success: function (data) {
                console.log(data)
            },
            error: function () {
                console.log('ee')
            }
        })
    }
</script>
}

Then, access the backend.然后,访问后端。

在此处输入图片说明

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

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