简体   繁体   English

Asp.Net MVC:从具有值列表的表单集合中插入数据

[英]Asp.Net MVC : Insert data from form collection with List of values

As you can see in picture, I have a form where I continuously add items to the table below. 如您在图片中看到的,我有一个表格,在其中我不断向下表添加项目。

When I click "Save all" button, it posts all the table values to " InsertBulk " method. 当我单击“全部保存”按钮时,它将所有表值发布到“ InsertBulk ”方法中。 在此处输入图片说明

And this is what I did in my view. 这就是我的观点。 I am created a form within the table. 我在表格中创建了一个表格。 Set name and values for each input field. 为每个输入字段设置名称和值。 Made the input fields hidden, displayed only text and then on clicking save all button it posts all those value to the InsertBulk method. 隐藏输入字段,仅显示文本,然后单击“ 保存所有”按钮,将所有这些值发布到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> 

This is my InsertBulk method. 这是我的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(); } 

Result : Ans this is what I get in Result. 结果:这就是我在结果中得到的。 How am I suppose to solve this ? 我应该如何解决呢? Please help! 请帮忙!

在此处输入图片说明

I also tried to count keys and loop through each in the InsertBulk method. 我还尝试计算键并在InsertBulk方法中循环遍历每个键。 But I think I did it all wrong. 但是我想我做错了。

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

If it is so why not split contents based on comma, save each of them in two different string array. 如果是这样,为什么不基于逗号分割内容,请将它们分别保存在两个不同的字符串数组中。 Then loop through each array's item saving name and description on each loop. 然后循环遍历每个数组的项目保存名称和描述。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM