Home > Software engineering >  Accessing ViewBag data from Controller
Accessing ViewBag data from Controller

Time:12-22

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"  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

CodePudding user response:

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

CodePudding user response:

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" >  </>

}

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

  • Related