【问题标题】:Asp.Net MVC : Insert data from form collection with List of valuesAsp.Net MVC:使用值列表从表单集合中插入数据
【发布时间】:2015-09-11 17:43:46
【问题描述】:

正如你在图片中看到的,我有一个表格,我不断地向下表中添加项目。

当我单击“全部保存”按钮时,它会将所有表值发布到“InsertBulk”方法。

在我看来,这就是我所做的。我在表格中创建了一个表格。为每个输入字段设置名称和值。隐藏输入字段,仅显示文本,然后单击 save all 按钮将所有这些值发布到 InsertBulk 方法。

@model FYPPharmAssistant.Models.InventoryModel.Manufacturer

@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@using (Html.BeginForm()) 
{ 
    <label>Name</label><br />
    @Html.EditorFor(m => m.ManufacturerName, new { htmlAttributes = new { @class = "form-control" } })  <br />
    <label>Description</label><br />
    @Html.EditorFor(m => m.Description, new { htmlAttributes = new { @class = "form-control" } })
    <input type="submit" id="addmore" value="add" />
}
@using (Html.BeginForm("InsertBulk", "Manufacturer"))
{
    <table id="table">
        <tr>
            <th>
                name &nbsp; &nbsp; 
            </th>
            <th>
                description
            </th>
        </tr>
    </table>
    <input type="submit" id="btnsaveall" value="Save all" />
}

<script>
    $(document).on('ready', function () {
        $('#addmore').on('click', function () {
            var $table = $("#table");            
            $table.append("<tr> <td><input type='hidden' name='ManufacturerName' value='" + $('#ManufacturerName').val() + "' />" + $('#ManufacturerName').val() + "</td> <td><input type='hidden' name='Description' value='" + $('#Description').val() + "'>" + $('#Description').val() + "</td> <td><a href='javascript:void(0)' onclick='removeItem(this)'>Remove</a></td></tr>");

            return false;
        });
      });
  </script>

这是我的 InsertBulk 方法。

[HttpPost]
        public void InsertBulk(FormCollection coll)
        {
           
                Manufacturer m = new Manufacturer();
                m.ManufacturerName = coll["ManufacturerName"];
                m.Description = coll["Description"];
                db.Manufacturers.Add(m);
                db.SaveChanges();
          
          }

结果: Ans 这就是我在结果中得到的。我想怎么解决这个问题?请帮忙!

我还尝试在 InsertBulk 方法中计算键并循环遍历每个键。但我觉得我做错了。

 int count = coll.Count;
            

              if(count == 0)
              {
                  return View("ChamForm", "Test");
              }
              else
              {
                  for(int i = 0; i<count; i++)
                  {
                      Manufacturer m = new Manufacturer();
                      m.ManufacturerName = coll["ManufacturerName[" + i + "]"];
                      m.Description = coll["Description[" + i + "]"];
                      db.Manufacturers.Add(m);
                      db.SaveChanges();
                  }
              }*

【问题讨论】:

    标签: c# asp.net-mvc formcollection


    【解决方案1】:

    如果是这样,为什么不根据逗号拆分内容,将它们中的每一个保存在两个不同的字符串数组中。然后在每个循环中遍历每个数组的项目保存名称和描述。

    【讨论】:

      【解决方案2】:
      public ActionResult Student(StudentModel model, FormCollection frm)
          {
              string XmlData = "<Parent>";
      
      
              var stuclass = frm.GetValues("stuclass");
              var InstituteId = frm.GetValues("Institute");
              var obtmark = frm.GetValues("obtmark");
              var totalmark = frm.GetValues("totalmark");
              var per = frm.GetValues("per");
      
              int count = stuclass.Count();
      
              for (int i = 0; i < count; i++)
              {
                  XmlData += "<child><stuclass>" + stuclass[i] + "</stuclass>"
                                  + "<InstituteId>" + InstituteId[i] + "</InstituteId>"
                                   + "<obtmark>" + obtmark[i] + "</obtmark>"
                                   + "<totalmark>" + totalmark[i] + "</totalmark>"
                                   + "<per>" + per[i] + "</per>"
                                     + "</child>";
              }
              XmlData += "</Parent>";
              model.XmlData = XmlData;
              var res = studal.Insertdtl(model);
      
              return View();
          }
      

      【讨论】:

      • 不鼓励使用纯代码的答案。请单击edit 并添加一些词来总结您的代码如何解决问题,或者解释您的答案与之前的答案/答案有何不同。谢谢
      猜你喜欢
      • 2013-06-12
      • 2013-06-21
      • 2021-05-02
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多