Avishekh Bharati
Avishekh Bharati

Reputation: 1918

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. enter image description here

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.

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

[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!

enter image description here

I also tried to count keys and loop through each in the InsertBulk method. 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();
                  }
              }*

Upvotes: 1

Views: 8022

Answers (2)

Meera
Meera

Reputation: 1

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

Upvotes: 0

user3486099
user3486099

Reputation:

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.

Upvotes: 1

Related Questions