【问题标题】:How to get value of single input field and sending with each products to database如何获取单个输入字段的值并将每个产品发送到数据库
【发布时间】:2018-12-22 02:32:09
【问题描述】:

我有一个页面,我在其中添加一个或多个产品(这部分正在工作),并将包括(产品名称、序列号、数量)在内的产品添加到数据库中。正如您在屏幕截图 中看到的那样,我有一个输入字段客户名称,我想获取客户名称输入字段的值并将每个产品发送到数据库。

数据插入数据库时​​的示例:

客户名称 |产品名称 |序列号 |数量

stackoverflow a 1234 1

Stackoverflow            B                                                                                                                                                                                                却也却也却却却却却却却却却却却却却又如此的丰富了起来 2


但是,现在在我的数据库中插入数据时看起来像这样:

客户名称 |产品名称 |序列号 |数量

null a 1234 1

null b 4567 2

说实话,我不知道如何在每个产品尝试将数据插入数据库时​​发送客户名称输入字段的值。谁能帮助我或指出我正确的方向!在此先感谢:)

控制器:

[HttpPost]
public JsonResult ProcessCreateRMA(CreateRMAVM vm)
{
    using (var namespace = new namespace())
    {
        if (vm.vares == null)
        {
            vm.vares = new List<CreateRMAVM.vare>();
        }

        foreach (var item in vm.vares)
        {
            var rmainsert = new RMA_History
            {
                //CustomerName = item.CustomerName,
                ProductName = item.ProductName,
                Serialnumber = item.Serialnumber,
                Qty = item.Qty,

            };

            db.RMA_History.Add(rmainsert);
        }
            db.SaveChanges();
        }

        return Json(vm, JsonRequestBehavior.AllowGet);

    }

JavaScript:

<script>
    $(document).ready(function () {
        //Add input field 
        var i = 0;
        $("#add").click(function (e) {
            i++;
            e.preventDefault();
            $("#tbhold").append('<tr id="row' + i + '"><td><div><input type="text" name="vares[' + i + '].ProductName" id=' + i + ' /></div></td><td><div><input type="text" name="vares[' + i + '].SerialNumber" id=' + i + '/></div></td><td><div style="padding:0" class="col-md-12"><input id="Qty" name="vares[' + i + '].Qty"/></div></td><td><button type="button" class="btn btn-danger btn_remove" id="' + i + '" name="remove"><i class="fa fa-minus-circle" aria-hidden="true"></i>Remove</button></td></tr>');
        });

        //Remove input field 
        $(document).on('click', '.btn_remove', function () {
            var button_id = $(this).attr("id");
            $("#row" + button_id + '').remove();
        });

    //Save to db by click
    $("#submit").click(function (e) {
        e.preventDefault();

        $.ajax({
            type: 'POST',
            url: '@Url.Action("ProcessCreateRMA", "User")',
            dataType: 'json',
            data: ($('#add_rma').serialize()),
            success: function (result) {
                console.log(result);
            },
            error: function () {
            console.log('something went wrong - debug it!');
            }
        });
    });

 });
</script>

查看:

<label>Customer Name</label>
<input type="text" name="CustomerName" id="CustomerName">

<form name="add_rma" id="add_rma">
    <table id='tbhold' class="table">
        <thead>
            <tr>
                <th>Product Name </th>
                <th>Serial number</th>
                <th>Qty</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <div>
                        <input type="text" name="vares[0].ProductName" id="ProductName" />
                    </div>

                </td>
                <td>
                    <div>
                        <input type="text" name="vares[0].Serialnumber" id="Serialnumber" />
                    </div>
                </td>
                <td>
                    <div>
                        <input name="vares[0].Qty" id="Qty"/>
                    </div>
                </td>

                <td>
                    <button type="button" name="add" id="add">Add more</button>
                </td>
            </tr>
        </tbody>
    </table>
    <input type="submit" name="submit" id="submit" value="Submit" />
</form>

视图模型:

public class CreateRMAVM
{
   public List<vare> vares { get; set; }

    public class vare
    {

        public vare()
        {

        }

        public vare(/*string CustomerName*/ string ProductName, string SerialNumber, string Qty)
        {
        }

        //public string CustomerName { get; set; }
        public string ProductName { get; set; }
        public string SerialNumber { get; set; }
        public string Qty { get; set; }
    }
 }

【问题讨论】:

    标签: javascript c# jquery ajax


    【解决方案1】:

    我已更新您的代码以使其正常工作。如果您使用的是数据网格,我可能会建议您使用其中一个,而不是尝试自己重建它。 jqGrid (http://www.trirand.com/blog/) 是一个流行的

    1. 将 CompanyName 留在表单中

    2. 这样更新您的模型。我已将 Customer 包含在 CreateRMAVM 和 vare 中

      public class CreateRMAVM
      {
      public List<vare> vares { get; set; }
      public string CustomerName { get; set; }
      
      public class vare
      {
      
      public vare()
      {
      
      }
      
      public vare(/*string CustomerName*/ string ProductName, string SerialNumber, string Qty)
      {
      }
      
      public string CustomerName { get; set; }
      public string ProductName { get; set; }
      public string SerialNumber { get; set; }
      public string Qty { get; set; }
      }
       }
      
      1. 更新您的控制器。 CustomerName 将填充到 CreateRMAVM 中,然后将一行代码复制到变量列表中

      [HttpPost] public JsonResult Create(CreateRMAVM vm) { try { if (vm.CustomerName != null && vm.vares != null) { vm.vares.Select(c => { c.CustomerName = vm.CustomerName; return c; }).ToList(); }

    【讨论】:

    • 我之前尝试过,如果我在表单标签内移动客户姓名输入,它将仅为第一行序列化输入,而不是当我添加更多行时,它将不再是客户输入字段,而不是我应该添加客户姓名输入到我的 Javascript 中(我通过 javaScript 动态添加更多行或输入),因此每次我添加更多输入字段时它都会添加客户输入字段,而不是我在寻找。我只寻找一个客户名称输入字段,而不是每次添加更多产品时发送客户名称字段的值。
    【解决方案2】:
    <input type="text" name="CustomerName" id="CustomerName">
    

    不在您的表单定义中。将其移动到表单中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2014-11-07
      • 1970-01-01
      相关资源
      最近更新 更多