skip to Main Content

I’m still learning MVC and I don’t know if it’s possible or not, In my application, I’m doing an excel upload task.

So here Excel uploaded and I’m adding to those excel data to view bag and shows in the view.


for (int i = 2; i <= range.Rows.Count; i++)
{
 try
   {
    M_Employee emp = new M_Employee();
    string comName = ((Microsoft.Office.Interop.Excel.Range)range.Cells[i, 1]).Text;
    var tempCompanyId = (from c in db.CreateCompany where c.CompanyName == comName select c.Id).First();
    emp.CompanyId = tempCompanyId;
    emp.EmpNo = int.Parse(((Microsoft.Office.Interop.Excel.Range)range.Cells[i, 2]).Text);
    emp.EmpName = ((Microsoft.Office.Interop.Excel.Range)range.Cells[i, 3]).Text;
    string dep = ((Microsoft.Office.Interop.Excel.Range)range.Cells[i, 4]).Text;
    var tempDepId = (from d in db.CreateDepartment where d.Department == dep select d.Id).First();
    emp.DepId = tempDepId;
    string dessig = ((Microsoft.Office.Interop.Excel.Range)range.Cells[i, 5]).Text;
    var tempDessId = (from h in db.CreateDesignation where h.Designation == dessig select h.Id).First();
    emp.DesignId = tempDessId;
    employees.Add(emp);
   }
   catch (Exception ex)
   {
     ViewBag.Error = "Error in " + i + " record";
     return View("Import");
   }
                       
}
if (employees !=null)
{
 ViewBag.EmpList = employees;
 return View("Import");
}


In the view, It shows the excel imported data the user.

So to upload these data to the database table, I have created a button with mapping the upload action result in the view

<input type="button" value="Upload" class="btn btn-success" onclick="location.href='@Url.Action("UploadEmployees", "M_Employee")'" />

In that Action I tried to call those ViewBag.EmpList to get the same data and pass to the table.

foreach (var item in ViewBag.EmpList)
{
  int ComId = item.CompanyId;
  int EmpNo = item.EmpNo;
  string EmpName = item.EmpName;
  int DepId = item.DepId;
  int DesId = item.DesignId;
}

But there I’m getting an error viewbag value is null. So is there any other way to do this?

Thanks

Editing–

This is my view

@{
ViewBag.Title = "Import";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
   @using (Html.BeginForm("Import", "M_Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
   <div class="card card-primary">
      <div class="card-header">
         <h1 class="card-title"><b>Upload Employees from Excel</b></h1>
      </div>
      <!-- /.card-header -->
      <div>
         <br />
         @Html.Raw(ViewBag.Error)
         <h4><span>Select Excel File</span></h4>
         <input type="file" name="excelFile" class="btn btn-warning"  />
         <br />
      </div>
      <div class="row">
         <div class="col-md-1">
            <br />
            <br />
            <input type="submit" value="Import" class="btn btn-info" />
         </div>
         <div class="col-md-1">
            <br />
            <br />
            <input type="button" value="Upload" class="btn btn-success" onclick="location.href='@Url.Action("UploadEmployees", "M_Employee")'" />
         </div>
      </div>
      <div class="card-body p-0">
         <table class="table table-striped">
            <tr>
               <th>Company</th>
               <th>EmpId</th>
               <th>EmpName</th>
               <th>Department</th>
               <th>Dessignation</th>
            </tr>
            @if (ViewBag.EmpList != null)
            {
            foreach (var item in ViewBag.EmpList)
            {
            <tr>
               <td>
                  @item.CompanyId
               </td>
               <td>
                  @item.EmpNo
               </td>
               <td>
                  @item.EmpName
               </td>
               <td>
                  @item.DepId
               </td>
               <td>
                  @item.DesignId
               </td>
            </tr>
            }
            }
         </table>
      </div>
      <!-- /.card-body -->
   </div>
   }
</div>

This is the view model I have created.

[NotMapped]
public class EmpExcelUploadViewModel
{
public int CompanyId { get; set; }
public int EmpNo { get; set; }
public string EmpName { get; set; }
public int EmpDep { get; set; }
public int EmpDes { get; set; }
}

2

Answers


  1. You can use ViewBag or ViewData for transferring data only one-way from Controller to View.

    You should resend data from client’s browser to your back-end with form.

    Or if user can not edit data you can use any of:

    • Save file on the first step, then use the identifier of uploaded earlier file, re-read it for saving
    • Parse file and save parsed data into storage as draft on the first step, then just remove draft mark from data on the second step
    Login or Signup to reply.
  2. You can not use ViewBag to move data from a client computer to a server. You can use Session or TempData (if the data is small) to keep data in the server, but I don’ t recommend to use it since it affects app scalability.

    You have 2 ways

    1. Repeat downloading in your upload action and save data to a database

    2. or you have to fix the action, using model instead of viewbag

    ....
    if (employees !=null)
    {
      return View("Import", employees);
    }
    else ... you error code
    

    in Import view add form and replace an ancor by submit button

    @model List<Employee>
    
    @using (Html.BeginForm("UploadEmployees", "M_Employee", FormMethod.Post))
    {
    .... emloyee input controls
    
    <button type="submit" value="Upload" class="btn btn-success">  </>
    
    }
    

    in UploadEmployees action you can get data from an input parameter and save to database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search