代码之家  ›  专栏  ›  技术社区  ›  Amiel Echizen

如何在JSON中以数组形式保存数据

  •  0
  • Amiel Echizen  · 技术社区  · 5 年前

    目前,我正在做一些项目,需要在多个数据中查看,然后才能保存到数据库中。但是,我在查找错误时遇到了问题,因为数组中没有返回导致数据返回null的任何值。

    *DetailId是主键,不为null。

    结果如下:我可以在列表中添加框细节,然后才能将其保存到数据库中。 enter image description here

    public partial class tmp_BoxDetail
        {
            //public int DetailId { get; set; }
            public string BoxDetailCode { get; set; }
            public string BoxDetailName { get; set; }
            public bool IOEnable { get; set; }
            public int IOAddress { get; set; }
            public int IOPin { get; set; }
            public string Status { get; set; }
            public string Remark { get; set; }
        }
    

    我的部分课程:

    [MetadataType(typeof(fnb_BoxDetailMetadata))]
        public partial class fnb_BoxDetail
        {
            public static string Concat(List<fnb_BoxDetail> enumerable)
            {
                return String.Join(ConcatenateHelper.RecordSeparator.ToString(), enumerable.Select(c => (
                      c.DetailId.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + c.BoxDetailCode.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + c.BoxDetailName.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + (c.IOEnable ? "1" : "0") + ConcatenateHelper.UnitSeparator.ToString()
                    + c.IOAddress.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + c.IOPin.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + c.Status.ToString() + ConcatenateHelper.UnitSeparator.ToString()
                    + c.Remark.ToString())).ToArray());
            }
        }
    

    @model XXX.Models.ViewModels.fnb_BoxViewModel
    
     <div class="form-group">
    
                <a href="#fnb_BoxDetail" class="btn btn-info" data-toggle="collapse" style="margin-bottom:10px; width:100%;">Box Detail</a>
               <div id="fnb_BoxDetail" class="collapse">
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.BoxDetailCode, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.BoxDetailCode, new { htmlAttributes = new { @class = "form-control", id = "BoxDetailCode" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.BoxDetailCode, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.BoxDetailName, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.BoxDetailName, new { htmlAttributes = new { @class = "form-control", id = "BoxDetailName" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.BoxDetailName, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.IOEnable, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.IOEnable, new { htmlAttributes = new { @class = "checkbox", id = "IOEnable" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.IOEnable, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.IOAddress, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.IOAddress, new { htmlAttributes = new { @class = "form-control", id = "IOAddress" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.IOAddress, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.IOPin, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.IOPin, new { htmlAttributes = new { @class = "form-control", id = "IOPin" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.IOPin, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.Status, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.DropDownListFor(model => model.fnb_BoxDetail.Status, ViewData["BoxDetailStatus"] as List<SelectListItem>, "-- Please Select Status --", new { @class = "form-control", id = "BoxDetailStatus" })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.Status, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div class="form-group">
                       @Html.LabelFor(model => model.fnb_BoxDetail.Remark, htmlAttributes: new { @class = "control-label col-md-2" })
                       <div class="col-md-10">
                           @Html.EditorFor(model => model.fnb_BoxDetail.Remark, new { htmlAttributes = new { @class = "form-control", id = "BoxDetailRemark" } })
                           @Html.ValidationMessageFor(model => model.fnb_BoxDetail.Remark, "", new { @class = "text-danger" })
                       </div>
                   </div>
    
                   <div id="partialBoxDetail">
                       @Html.Partial("_BoxDetailList")
                   </div>
               </div>
    

    我在_BoxDetailList.cshtml中的代码如下:

    @model XXX.Models.ViewModels.fnb_BoxViewModel
    
    
    
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @*<input id="buttonPackage" name="AddPackage" value="Add Package" type="button" class="btn btn-default btn-primary" />*@
            <input id="buttonBoxDetail" name="AddBoxDetail" value="Add Box Detail" type="button" class="btn btn-default btn-primary" />
        </div>
    </div>
    
    <div class="">
        <table id="BoxDetailTable" class="table table-bordered table-responsive table-hover" style="width:80%; margin:0 auto;">
                      <tr style="background-color:#2fa4e7;">
                          <th>Box Detail Code</th>
                          <th>Box Detail Name</th>
                          <th>IOEnable</th>
                          <th>IOAddress</th>
                          <th>IOPin</th>
                          <th>Status</th>
                          <th>Remark</th>
                          <th style="width:100px"></th>
                    </tr>
            @for (int i = 0; i < Model.fnb_Box.fnb_BoxDetail.Count(); i++)
            {   <tr>
                    <td class="attBoxDetailCode">@Model.fnb_Box.fnb_BoxDetail[i].BoxDetailCode</td>
                    <td class="attBoxDetailName">@Model.fnb_Box.fnb_BoxDetail[i].BoxDetailName</td>
                    <td class="attIOEnable">@Model.fnb_Box.fnb_BoxDetail[i].IOEnable</td>
                    <td class="attIOAddress">@Model.fnb_Box.fnb_BoxDetail[i].IOAddress</td>
                    <td class="attIOPin">@Model.fnb_Box.fnb_BoxDetail[i].IOPin</td>
                    <td class="attStatus" style="display:none;">@Model.fnb_Box.fnb_BoxDetail[i].Status</td>
                    <td>@Model.fnb_Box.fnb_BoxDetail[i].Status</td>
                    <td class="attRemark">@Model.fnb_Box.fnb_BoxDetail[i].Remark</td>
                    <td> <a class="btn btn-default btn-primary">Remove</a></td>
                </tr>
            }
        </table>
    
        <table id="HiddenBoxDetailTable" class="table table-bordered table-responsive table-hover" style="width:80%; margin:0 auto; display:none">
            <tr style="background-color:#2fa4e7;">
                <th>BoxDetailCode</th>
                <th>BoxDetailName</th>
                <th>IOEnable</th>
                <th>IOAddress</th>
                <th>IOPin</th>
                <th>Status</th>
                <th>Remark</th>
                <th>@*DetailId*@</th>
            </tr>
            @for (int i = 0; i < Model.fnb_Box.fnb_BoxDetail.Count(); i++)
            {
                <tr>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].BoxDetailCode, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].BoxDetailName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].IOEnable, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].IOAddress, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].IOPin, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].Status, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    <td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].Remark, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>
                    @*td>@Html.EditorFor(m => Model.fnb_Box.fnb_BoxDetail[i].DetailId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })</td>*@
                </tr>
            }
        </table>
    </div>
    
    <script>
        var BDArray = [];
        $(function () {
            $("#buttonBoxDetail").click(function () {
    
                $('#BoxDetailTable tr').each(function (a, b) {
                    var bdc = $('.attBoxDetailCode', b).text();
                    var bdn = $('.attBoxDetailName', b).text();
                    var ioe = $('.attIOEnable', b).text();
                    var ioa = $('.attIOAddress', b).text();
                    var iop = $('.attIOPin', b).text();
                    var stat = $('.attStatus', b).text();
                    var rema = $('.attRemark', b).text();
                    BDArray.push({ BoxDetailCode: bdc, BoxDetailName: bdn, IOEnable: ioe, IOAddress: ioa, IOPin: iop, Status: stat, Remark: rema, });
    
                })
    
                var valbdc = $("#BoxDetailCode").val();
                var valbdn = $("#BoxDetailName").val();
                var valioe = $("#IOEnable").prop('checked');
                var valioa = $("#IOAddress").val();
                var valiop = $("#IOPin").val();
                var valstat = $("#BoxDetailStatus").val();
                var valrema = $("#BoxDetailRemark").val();
                $.ajax({
                    url: "AddBoxDetail",
                    type: "get",
                    data: { boxdetailcode: valbdc.toString(), boxdetailname: valbdn.toString(), ioenable: valioe.toString(), ioaddress: valioa.toString(), iopin: valiop.toString(), boxdetailstatus: valstat.toString(), boxdetailremark: valrema.toString(), BDarrayList:JSON.stringify(BDArray)},
                    success: function (result) {
                        $("#partialBoxDetail").html(result);
                    }
                });
    
            });
        });
    
        var index, tableList = document.getElementById('BoxDetailTable');
        for (var i = 1; i < table1.rows.length; i++) {
            tableList.rows[i].cells[8].onclick=function(){
                var c = confirm("Do you want to delete this row?");
                if (c === true) {
                    index = this.parentElement.rowIndex;
                    tableList.deleteRow(index); 
    
                    $('#BoxDetailTable tr').each(function (a, b) {
                        var bdc = $('.attBoxDetailCode', b).text();
                        var bdn = $('.attBoxDetailName', b).text();
                        var ioe = $('.attIOEnable', b).text();
                        var ioa = $('.attIOAddress', b).text();
                        var iop = $('.attIOPin', b).text();
                        var stat = $('.attStatus', b).text();
                        var rema = $('.attRemark', b).text();
                        BDArray.push({BoxDetailCode: bdc, BoxDetailName: bdn, IOEnable: ioe, IOAddress: ioa, IOPin: iop, Status: stat, Remark: rema, });
    
                    })
    
                    var valbdc = "";
                    var valbdn = "";
                    var valioe = "";
                    var valioa = "";
                    var valiop = "";
                    var valstat = "";
                    var valrema = "";
                    $.ajax({
                        url: "AddBoxDetail",
                        type: "get",
                        data: { boxdetailcode: valbdc.toString(), boxdetailname: valbdn.toString(), ioenable: valioe.toString(), ioaddress: valioa.toString(), iopin: valiop.toString(), boxdetailstatus: valstat.toString(), boxdetailremark: valrema.toString(), BDarrayList: JSON.stringify(BDArray) },
                        success: function (result) {
                            $("#partialBoxDetail").html(result);
                        }
                    });
                }
            };
        }
        var $ = jQuery.noConflict();
    </script>
    

    我的数据库设计如下:

    父数据库--> enter image description here

    子数据库--> enter image description here

    我的控制器代码如下:

    public ActionResult AddBoxDetail(string boxdetailcode, string boxdetailname, string ioenable, string ioaddress, string iopin, string boxdetailstatus, string boxdetailremark, string BDarrayList)
            {
    
                modelMain.fnb_Box = new fnb_Box();
                modelMain.fnb_Box.fnb_BoxDetail = new List<fnb_BoxDetail>();
    
                var list = JsonConvert.DeserializeObject<List<tmp_BoxDetail>>(BDarrayList);
    
                if (boxdetailcode != "" && boxdetailname != "" && ioenable != "" && ioaddress != "" && iopin != "" && boxdetailstatus != "" && boxdetailremark != "")
                {
    
                    foreach (var item in list)
                    {
                        if (modelMain.fnb_Box.BoxId != 0)
                                modelMain.fnb_Box.BoxId = Convert.ToInt32(modelMain.fnb_Box.BoxId);
                            else
                                modelMain.fnb_Box.BoxId = 0;
                        modelMain.fnb_BoxDetail.DetailId=_service.getNextDetailId(modelMain.fnb_Box.BoxId);
    
                        if (item.BoxDetailCode != "")
                        {
                            fnb_BoxDetail child_fnb_BoxDetail = new fnb_BoxDetail();
                            child_fnb_BoxDetail.BoxDetailCode = item.BoxDetailCode;
                            child_fnb_BoxDetail.BoxDetailName = item.BoxDetailName;
                            child_fnb_BoxDetail.IOEnable = Convert.ToBoolean(item.IOEnable);
                            child_fnb_BoxDetail.IOAddress = Convert.ToInt32(item.IOAddress);
                            child_fnb_BoxDetail.IOPin = Convert.ToInt32(item.IOPin);
                            child_fnb_BoxDetail.Status = item.Status;
                            child_fnb_BoxDetail.Remark = item.Remark;
                            modelMain.fnb_Box.fnb_BoxDetail.Add(child_fnb_BoxDetail);
                        }
    
                    }
                    fnb_BoxDetail child_fnb_BoxDetailNew = new fnb_BoxDetail();
                    child_fnb_BoxDetailNew.BoxDetailCode = boxdetailcode;
                    child_fnb_BoxDetailNew.BoxDetailName = boxdetailname;
                    child_fnb_BoxDetailNew.IOEnable = Convert.ToBoolean(ioenable);
                    child_fnb_BoxDetailNew.IOAddress = Convert.ToInt32(ioaddress);
                    child_fnb_BoxDetailNew.IOPin = Convert.ToInt32(iopin);
                    child_fnb_BoxDetailNew.Status = boxdetailstatus;
                    child_fnb_BoxDetailNew.Remark = boxdetailremark;
                    modelMain.fnb_Box.fnb_BoxDetail.Add(child_fnb_BoxDetailNew);
                }
                else if ((boxdetailcode == "" || boxdetailname == "" || ioenable == "" || ioaddress == "" || iopin == "" || boxdetailstatus == "" || boxdetailremark == "") && list.Count() > 1)
                {
    
                    foreach (var item in list)
                    {
                        if (item.BoxDetailCode != "")
                        {
                            fnb_BoxDetail child_fnb_BoxDetail = new fnb_BoxDetail();
                            child_fnb_BoxDetail.BoxDetailCode = item.BoxDetailCode;
                            child_fnb_BoxDetail.BoxDetailName = item.BoxDetailName;
                            child_fnb_BoxDetail.IOEnable = Convert.ToBoolean(item.IOEnable);
                            child_fnb_BoxDetail.IOAddress = Convert.ToInt32(item.IOAddress);
                            child_fnb_BoxDetail.IOPin = Convert.ToInt32(item.IOPin);
                            child_fnb_BoxDetail.Status = item.Status;
                            child_fnb_BoxDetail.Remark = item.Remark;
                            modelMain.fnb_Box.fnb_BoxDetail.Add(child_fnb_BoxDetail);
                        }
    
                    }
                }
    
                return PartialView("_BoxDetailList", modelMain);
            }
    

    enter image description here

    0 回复  |  直到 5 年前
    推荐文章