简体   繁体   English

访问 controller 中的 model 数据视图

[英]Access view model data in controller

In my application I have created a view model to show data in the View.在我的应用程序中,我创建了一个视图 model 以在视图中显示数据。

I created view model as this.我这样创建了视图 model。

[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.在 controller 中,我已将数据分配给视图 model 并传递给视图。 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.我想知道,当用户点击上传按钮时,数据已经在视图 model 我想传递给数据库。 So how can I access the data already in the view model?那么如何访问 model 视图中已经存在的数据呢? I'm still learning MVC and I don't know if it's possible or not.我还在学习MVC,不知道有没有可能。

This is what I tried.这是我尝试过的。 Here in the empExcel getting error that EmpExcelUploadViewModel does not contain public instance.在 empExcel 中出现 EmpExcelUploadViewModel 不包含公共实例的错误。

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.与其在视图中保存数据,不如在 controller 中进行。

In the controller, if there's any error saving the data to the database, set a separate error flag indicating this.在 controller 中,如果将数据保存到数据库时出现任何错误,请设置一个单独的错误标志来指示这一点。 Then, the View uses this flag to determine what to display然后,View 使用此标志来确定要显示的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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