0

I have done a simple applicaion using ASP.NET MVC4. I am trying to insert multiple row data into the database.What i have done that is working fine for single row. Single row is inserted but in multiple row i am getting a problem... the data are not pass from client side to server side (catch by a form collection object )in controller and also loop is not properly working.

my view is:

<table style="width:100%" id="tbldata">
    <tr>
        <th>Document </th>
        <th>Sem/Year(ifapplicable)</th>
        <th>File</th>

    </tr>
@for (int i = 0; i < 7; i++ )
{

    <tr class="document">
        <td style="width:40%">@Html.DropDownListFor(m => m.selectdocId,                    ViewBag.selectdocId as IEnumerable<SelectListItem>,"--Select--", new { @id="docid" +i , @class="Doctype"})</td>
        <td style="width:10%">@Html.TextBoxFor(m => m.sem, new { @id="sem" +i, @class="semid"})</td>
        <td style="width:40%"><input type="file" multiple="" name="file" value="Browse" /></td>
    </tr>

}

  </table> 

My controller is:

  public ActionResult display(FormCollection collection, IEnumerable<HttpPostedFileBase>file )
    {
        for(int i=0; i<7; i++)
        {

          int semid = Convert.ToInt32(collection["sem" + i]);
          int docid = Convert.ToInt32(collection["docid" + i]);

            tbldocumentdetail doc = new tbldocumentdetail();
            doc.sem = Convert.ToInt32(semid);
            doc.selectdocId = Convert.ToInt32(docid);
            db.tbldocumentdetails.Add(doc);
             db.SaveChanges();
           }

             foreach (var item in file)
            {
                if (item == null && item.ContentLength < 0)
                {
                    ModelState.AddModelError("file", "please uploded your file");
                }
                else
                {
                    var filename = Path.GetFileName(item.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content/savedoc"),           filename);
                    item.SaveAs(path);

                    tbldocumentdetail doc = new tbldocumentdetail();
                     doc.fileName = filename;
                     string a = "~/Content/savedoc" + filename;
                     doc.path = a;
                    db.tbldocumentdetails.Add(doc);
                    db.SaveChanges(); 
                     }

1 Answers1

0

For this , Instead of using form collection you can use IEnumerable as the POST action parameter. You can refer below link

MVC Form not able to post List of objects

Community
  • 1
  • 1
Razack
  • 950
  • 3
  • 13
  • 26