简体   繁体   中英

Access view model data in controller

In my application I have created a view model to show data in the View.

I created view model as this.

[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;
  }

}

In the controller I have assigned data to the view model and passed to the view. Just to show the data to the user, there is no editing or deleting data from the user view. That stage is works fine.

This is the view

@model IEnumerable
<Asp_PASMVC.ViewModels.EmpExcelUploadViewModel>
@{
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>
      <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 Model)
            {
            <tr>
               <td>
                  @Html.DisplayFor(modelItem => item.CompanyId)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => item.EmpNo)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => item.EmpName)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => item.EmpDep)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => item.EmpDes)
               </td>
            </tr>
            }
            }
         </table>
      </div>
      <!-- /.card-body -->
   </div>
   }
</div>

I want to know, when user clicks to the Upload button, that the data already in the view model I want to pass to the database. So how can I access the data already in the view model? I'm still learning MVC and I don't know if it's possible or not.

This is what I tried. Here in the empExcel getting error that EmpExcelUploadViewModel does not contain public instance.

public ActionResult UploadEmployees(EmpExcelUploadViewModel empExcel) {
  try {

    foreach(var item in empExcel) // error in empExcel {
      int ComId = item.CompanyId;
      int EmpNo = item.EmpNo;
      string EmpName = item.EmpName;
      int DepId = item.EmpDep;
      int DesId = item.EmpDes;

      var isEmployeeExists = (from e in db.CreateEmployee where e.EmpNo == EmpNo select e.Id).First();

      if (isEmployeeExists != 0) {
        M_Employee e = new M_Employee();
        e.CompanyId = ComId;
        e.EmpNo = EmpNo;
        e.EmpName = EmpName;
        e.DepId = DepId;
        e.DesignId = DesId;
        e.Status = true;
        e.CreateDate = DateTime.Now;
        e.CreateBy = int.Parse(((System.Security.Claims.ClaimsIdentity) User.Identity).FindFirst("UserId").Value);
        db.CreateEmployee.Add(e);
        db.SaveChanges();
      } else {
        M_Employee m_Employee = db.CreateEmployee.First(x => x.Id == isEmployeeExists);
        m_Employee.DepId = DepId;
        m_Employee.DesignId = DesId;
        db.SaveChanges();
      }

    }
  } catch (Exception ex) {

    ViewBag.Error = "Error " + ex;
  }

  ViewBag.Error = "All Records uploaded to the database";
  ViewBag.EmpList = null;

  return View("Import");

}
}

Instead of saving data in the view, you should do it in the controller.

In the controller, if there's any error saving the data to the database, set a separate error flag indicating this. Then, the View uses this flag to determine what to display

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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