Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c# - How to save data in Array form in JSON

Currently I'm doing some project that need to be view in multiple data before it save be save into the database. However, I'm having trouble in findings the error since the array did not return any value in it that caused the data is return null.

*DetailId is a primary key and not null.

The result should be like below: I can add the box detail in the list before I can save it into the database. enter image description here

my tmp_BoxDetail:

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; }
    }

my partial classes:

[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());
        }
    }

my code in edit.cshtml & create.cshtml is as below:

@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>

my code in _BoxDetailList.cshtml is as below:

@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_BoxDetai

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...