简体   繁体   English

asp.net 核心绑定 null in controller post 请求(列表中包含内部元素列表)

[英]asp.net core binding null in controller post request (list with inside elements lists in view)

I have a view with a list of items that among their properties have a list of items (in View asp.net core).我有一个带有项目列表的视图,在它们的属性中有一个项目列表(在视图 asp.net 核心中)。

I use a generic list to have common properties that I use in the view.我使用通用列表来拥有我在视图中使用的通用属性。

The ViewModels are these: ViewModels是这些:

public class AgaqEleViewModel
{        
    public int CodEle { get; set; }

    public int AbrEle { get; set; }

    public string Valor { get; set; }

    ...
}

public class AgaqViewModel
{
    [Required(ErrorMessageResourceType = typeof(AguasResx.DataAnnotations), ErrorMessageResourceName = nameof(AguasResx.DataAnnotations.Required))]
    public int Hoja { get; set; } // Key Id compossed
    
    [Required(ErrorMessageResourceType = typeof(AguasResx.DataAnnotations), ErrorMessageResourceName = nameof(AguasResx.DataAnnotations.Required))]
    public DateTime FechaTomaAq { get; set; }
    
    public float? ProfmuestraM { get; set; }

    ...


    // List test
    public List<AgaqEleViewModel> AgaqEle { get; set; }


    ...
            
}

public class GenericListViewModel<T>
{

    public RespViewModel Resp { get; set; }

    public List<T> ItemsList { get; set; }


    public GenericListViewModel()
    {

    }

    public GenericListViewModel(RespViewModel resp, List<T> genericList)
    {
        Resp = resp;
        ItemsList = genericList;
    }

}

My Razor View is this:我的Razor 视图是这样的:

@model GenericListViewModel<AgaqViewModel>

@{
// get max number of columns of different elements 
int numColElements = Model.ItemsList.Select(s => s.AgaqEle.Count).Max();
}


<form asp-action="AgaqLoaded" method="post" id="agaqExcelForm">

    @if(Model.Resp.XXX)
    {
        ...
    }
    
    <table class="table table-bordered table-sm table-striped">
        <thead>
            <tr>
                @* header columns *@
                <th title="@Literals.Hoja">@Literals.Hoja</th>
                ...

                @* create elements columns header´s *@
                @for(int i = 0; i < numColElements; i++)
                {
                    <th>@Literals.Element</th>
                }
                ...


        <tbody>
            // iterate each item in Model list
            @for (var i = 0; i < Model.ItemsList.Count(); i++)
            {               
                <tr>                    
                    <td class="">
                        <input asp-for="@Model.ItemsList[i].Hoja" type="hidden" />
                        @Model.ItemsList[i].Hoja
                    </td>
                    <td>
                        <input asp-for="@Model.ItemsList[i].FechaTomaAq" type="hidden" />
                        @Model.ItemsList[i].FechaTomaAq
                    </td>
                    ...

                    // iterate each list elements in each item in Model list
                    @for(int j = 0; j < numColElements; j++)
                        {
                            <td>                                
                                <input asp-for="@Model.ItemsList[i].AgaqEle[j].CodEle" type="hidden" />
                                <input asp-for="@Model.ItemsList[i].AgaqEle[j].AbrEle" type="hidden" />
                                <input asp-for="@Model.ItemsList[i].AgaqEle[j].Valor" type="hidden" />
                                ...
                                @elemAgaq.AbrEle: @elemAgaq.Valor
                            </td>                           
                        }
            }
...
<tfoot>
<button id="btnLoad" type="submit" class="btn btn-sm btn-primary"><i class="fa fa-upload"></i>&nbsp;@AguasLiterals.LoadData</button>
    ...
</form>

and my Controller is this:我的Controller是这样的:

[HttpPost("{controller}/{action}/")]
public async Task<IActionResult> AgaqLoaded(GenericListViewModel<AgaqViewModel> listToLoad)
{   
    // save 
    foreach (AgaqViewModel agaqEx in listToLoad.ItemsList)
    {
        AgaqDto saveAgaq = null;
        AgaqDto agaq = _mapper.Map<AgaqDto>(agaqEx);
            
        // save Agaq
        saveAgaq = await _service.SetAgaqAsync(agaq);
        ...

The generated HTML code is:生成的HTML代码为:

<td>
    <input type="hidden" data-val="true" data-val-range="El valor ..." data-val-range-max="9999" data-val-range-min="1" data-val-required="Este campo es obligatorio." id="ItemsList_0__Hoja" name="ItemsList[0].Hoja" value="1921">
    1921
</td>
<td>
    <input type="hidden" data-val="true" data-val-required="Este campo es obligatorio." id="ItemsList_0__FechaTomaAq" name="ItemsList[0].FechaTomaAq" value="20/07/2021 0:00:00">
    20/07/2021 0:00:00 
</td>


<td>
    <input type="hidden" data-val="true" data-val-required="The CodEle field is required." id="ItemsList_0__AgaqEle_0__CodEle" name="ItemsList[0].AgaqEle[0].CodEle" value="3">
    <input type="hidden" id="ItemsList_0__AgaqEle_0__AbrEle" name="ItemsList[0].AgaqEle[0].AbrEle" value="Na">
    <input type="hidden" id="ItemsList_0__AgaqEle_0__Valor" name="ItemsList[0].AgaqEle[0].Valor" value="48">
    ...
    Na: 48
</td>
<td>
    <input type="hidden" data-val="true" data-val-required="The CodEle field is required." id="ItemsList_0__AgaqEle_1__CodEle" name="ItemsList[0].AgaqEle[1].CodEle" value="3">
    <input type="hidden" id="ItemsList_0__AgaqEle_1__AbrEle" name="ItemsList[0].AgaqEle[1].AbrEle" value="Ca">
    <input type="hidden" id="ItemsList_0__AgaqEle_1__Valor" name="ItemsList[0].AgaqEle[1].Valor" value="16">
    ...
    Ca: 16
</td>

The objects arrive correctly to the view and the generated HMTL is apparently correct.对象正确到达视图并且生成的 HTML 显然是正确的。 (The rest of the items/elements have the correct numbering: ItemsList_1__AgaqEle_0__CodEle, ItemsList_1__AgaqEle_0__AbrEle... ItemsList_7__AgaqEle_0__CodEle...) (项目/元素的 rest 具有正确的编号:ItemsList_1__AgaqEle_0__CodEle,ItemsList_1__AgaqEle_0__AbrEle ... ItemsList_7__AgaqEle_0__CodEle ...)

When in the view I comment out the part of the elements (second loop, asp-for hidden properties) inside each item in the list, I get it correctly.当在视图中注释掉列表中每个项目内的元素部分(第二个循环,asp-for 隐藏属性)时,我得到了正确的结果。

在此处输入图像描述

But if I don't comment the elements, although the HTML is apparently correct, the post request call arrives null without any item or elements.但是如果我不评论这些元素,虽然 HTML 显然是正确的,但发布请求调用到达 null 时没有任何项目或元素。

在此处输入图像描述

I have several "simple" lists of complex elements that work fine, but this list composed of items with a list of elements I can't binding it.我有几个工作正常的复杂元素的“简单”列表,但是这个列表由带有我无法绑定的元素列表的项目组成。

What am I doing wrong?我究竟做错了什么?

I've tried removing the generic viewmodel and leaving it as a list of items, I've tried using list[i][j] or list[i].[j] and everything I could think of but nothing works?我试过删除通用视图模型并将其保留为项目列表,我试过使用 list[i][j] 或 list[i].[j] 以及我能想到的一切但没有任何效果? Any ideas?有任何想法吗? Thanks.谢谢。

In the end, the problem was not the binding of the properties, but rather the fact that the information was not arriving because of the large volume of information (as it was a list of items with a list of elements for each item).最后,问题不在于属性的绑定,而是由于信息量大而导致信息未到达这一事实(因为它是一个项目列表,每个项目都有一个元素列表)。

Instead of giving an error with information, the input parameter of the binding simply arrived null .绑定的输入参数没有给出错误信息,而是简单地到达了 null

To solve it we have added in the startup.cs , in the ConfigureServices part:为了解决这个问题,我们在startup.csConfigureServices部分添加了:

services.Configure<FormOptions>(options =>
{
    options.ValueCountLimit = int.MaxValue;
});

I hope it helps someone else.我希望它能帮助别人。

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

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