C# 如何在html表单中表示json数组

C# 如何在html表单中表示json数组,c#,json,asp.net-core,aspnetboilerplate,C#,Json,Asp.net Core,Aspnetboilerplate,注意:我还使用了的模板和标记帮助程序,因此bc-属性和表单组标记是有效的 我正在尝试将模型发布到我的一个应用程序服务中。但是我有以下错误 “验证期间检测到以下错误。 ↵ - 无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.IList`1[App.Stores.StoreWalk.Dtos.createOreditStoreWalkCategoryTo]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 ↵

注意:我还使用了的模板和标记帮助程序,因此
bc-
属性和
表单组
标记是有效的

我正在尝试将模型发布到我的一个应用程序服务中。但是我有以下错误

“验证期间检测到以下错误。 ↵ - 无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.IList`1[App.Stores.StoreWalk.Dtos.createOreditStoreWalkCategoryTo]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 ↵要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为普通的.NET类型(例如,不是整数之类的基元类型,也不是数组或列表之类的集合类型)也可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 ↵路径“类别0”,第1行,位置84。 ↵"消息:“您的请求无效!“验证错误:[{…}]proto:对象

我理解错误的意思,只是不知道如何修复代码

在这两个建议中,
要修复此错误,我必须选择将JSON更改为JSON数组(例如[1,2,3])

可能是我的错,但我在玩我的代码,注意到当我从我的
name
属性中删除
@i
@j
时,我可以成功发布,但我的单选按钮失去了所有功能(由于命名冲突,单击第二行会更改第一行),而且,我认为需要索引来保持它们的唯一性(我还在学习,如果我错了,请纠正我)

im发回的json字符串是通过调用
$('form[name=StoreWalkForm]')创建的。serializeFormToObject()
如下所示,并利用

JSON 模范班
public类CreateOrEditStoreWalkDto:EntityDto
{
[必需]
公共字符串存储{get;set;}
公共字符串注释{get;set;}
公共字节[]签名{get;set;}
公共int版本{get;set;}
公共部门到部门{get;set;}
公共IList类别{get;set;}=new List();
}
公共类CreateOreEdit StoreWalkCategoryTo
{
公共类别数据到类别{get;set;}
公共IList问题{get;set;}=new List();
}
公共类CreateOreEdit StoreWalkQuestiondTo
{
公共问题到问题{get;set;}
公共回答回答{get;set;}
}
CSHtml

@if(Model.IsEditMode)
{
}
@如果(!Model.IsEditMode)
{
}


@对于(var i=0;i @Html.TextBoxFor(m=>m.StoreWalk.Categories[i].Questions[j].S3Files,new{type=“file”,multiple=“multiple”}) @Model.StoreWalk.Categories[i].问题[j].答案.Comments } } @Model.StoreWalk.Comments @L(“签字”) @Html.Partial(“~/Areas/Shared/Views/Shared/SignaturePad/Edit.cshtml”,新的SignaturePadModel() { Id=“签名”, 数据=Model.StoreWalk.Signature, FooterText=L(“签名”), 必需=真 })
{useintKeysaArrayIndex:true}
传递到
$form.serializeJSON()

$.fn.serializeFormToObject=函数(){
var$form=$(此);
变量字段=$form.find(“[disabled]”);
字段.prop('disabled',false);
//var json=$form.serializeJSON();
var json=$form.serializeJSON({useintKeysaArrayIndex:true});
字段.prop('disabled',true);
返回json;
};

问题中的链接:

$.fn.serializeFormToObject = function() {
    var $form = $(this);
    var fields = $form.find('[disabled]');
    fields.prop('disabled', false);
    var json = $form.serializeJSON();
    fields.prop('disabled', true);
    return json;
};
{
  "version": "2",
  "department": {
    "id": "8",
    "name": "ISF"
  },
  "store": "001",
  "categories": {
    "0": {
      "category": {
        "id": "4"
      },
      "questions": {
        "0": {
          "question": {
            "question": "SIGNAGE",
            "id": "233"
          },
          "answer": {
            "boolValue": "false",
            "comments": "1"
          }
        },
        "1": {
          "question": {
            "question": "PARKING LOT BLACKTOP",
            "id": "234"
          },
          "answer": {
            "boolValue": "true",
            "comments": "2"
          }
        }
      }
    }
  },
  "comments": "123",
  "signature": ""
}
public class CreateOrEditStoreWalkDto : EntityDto<int?>
{
    [Required]
    public string Store { get; set; }

    public string Comments { get; set; }

    public byte[] Signature { get; set; }

    public int Version { get; set; }

    public DepartmentsDto Department { get; set; }

    public IList<CreateOrEditStoreWalkCategoryDto> Categories { get; set; } = new List<CreateOrEditStoreWalkCategoryDto>();
}

public class CreateOrEditStoreWalkCategoryDto
{
    public CategoriesDto Category { get; set; }
    public IList<CreateOrEditStoreWalkQuestionDto> Questions { get; set; } = new List<CreateOrEditStoreWalkQuestionDto>();
}

public class CreateOrEditStoreWalkQuestionDto
{
    public QuestionsDto Question { get; set; }
    public AnswersDto Answer { get; set; }
}
    <form name="StoreWalkInformationsForm" role="form" enctype="multipart/form-data" novalidate class="form-validation">

        @if (Model.IsEditMode)
        {
            <input type="hidden" name="id" value="@Model.StoreWalk.Id" />
        }

        <input type="hidden" name="version" value="@Model.StoreWalk.Version" />
        <input type="hidden" name="department[id]" value="@Model.StoreWalk.Department.Id" />
        <input type="hidden" name="department[name]" value="@Model.StoreWalk.Department.Name" />

        <form-group>
            <select id="StoreWalk_Store" name="store" bc-label="@L("Store")" required="required" bc-required="true" bc-validation="true" value="@Model.StoreWalk.Store" maxlength="@App.Stores.StoreWalk.EntriesConsts.MaxStoreLength" minlength="@App.Stores.StoreWalk.EntriesConsts.MinStoreLength">
                @if (!Model.IsEditMode)
                {
                    <option value="" selected="selected"></option>
                }
            </select>
        </form-group>

        <br /><br />

        @for (var i = 0; i < Model.StoreWalk.Categories.Count; i++)
        {
            <h5 class="m--font-primary">@Model.StoreWalk.Categories[i].Category.Name</h5>
            <input type="hidden" name="categories[@i][category][id]" value="@Model.StoreWalk.Categories[i].Category.Id" />

            <table bc-responsive="true">
                <thead>
                    <tr>
                        <th>#</th>
                        <th scope="col">Question</th>
                        <th scope="col"></th>
                        <th scope="col">Comments</th>
                    </tr>
                </thead>

                <tbody>
                    @*@for (var j = 0; j < Model.StoreWalk.Categories[i].Questions.Count; j++)*@
                        @for (var j = 0; j < 2; j++)
                        {
                            <tr>
                                <td>@(q.Next()).</td>
                                <td scope="row">
                                    <form-group>
                                        <input type="hidden" name="categories[@i][questions][@j][question][question]" value="@Model.StoreWalk.Categories[i].Questions[j].Question.Question" bc-label="@Model.StoreWalk.Categories[i].Questions[j].Question.Question" />
                                        <input type="hidden" name="categories[@i][questions][@j][question][id]" value="@Model.StoreWalk.Categories[i].Questions[j].Question.Id" />
                                        <br />
                                        @Html.TextBoxFor(m => m.StoreWalk.Categories[i].Questions[j].S3Files, new { type = "file", multiple = "multiple" })
                                    </form-group>
                                </td>
                                <td>
                                    <form-group>
                                        <radio-list bc-required="true" bc-validation="true" id="categories[@i][questions][@j][answer][boolValue]">
                                            <input type="radio" name="categories[@i][questions][@j][answer][boolValue]" value="true" bc-label="@L("Yes")" required="required" />
                                            <input type="radio" name="categories[@i][questions][@j][answer][boolValue]" value="false" bc-label="@L("No")" required="required" />
                                        </radio-list>
                                    </form-group>
                                </td>
                                <td>
                                    <form-group>
                                        <textarea name="categories[@i][questions][@j][answer][comments]" rows="3">@Model.StoreWalk.Categories[i].Questions[j].Answer.Comments</textarea>
                                    </form-group>
                                </td>
                            </tr>
                        }
                </tbody>
            </table>
        }


        <form-group>
            <textarea class="form-control" id="StoreWalk_Comments" type="text" name="comments" bc-label="@L("Comments")" rows="4">@Model.StoreWalk.Comments</textarea>
        </form-group>

        <form-group>
            <label for="signature">@L("Signature")</label>
            @Html.Partial("~/Areas/Shared/Views/Shared/SignaturePad/Edit.cshtml", new SignaturePadModel()
            {
                Id = "signature",
                Data = Model.StoreWalk.Signature,
                FooterText = L("Signature"),
                Required = true
            })
        </form-group>
    </form>