0

我正在从客户端发送一些视图模型数据并想更新模型。我知道如何通过调用存储过程来做到这一点,但非常想知道如何使用 linq 查询来做到这一点。任何帮助将不胜感激。谢谢

[HttpPost]
         public JsonResult EditEmployee(Models.Employee employee) 
         {
             try
             {
                 if (ModelState.IsValid)
                 {
                     using (emsCtx)
                     {
                         var employeeResults = (from q in emsCtx.Employees
                                                where q.Id == employee.Id
                                                //code to update the model.
                                                );                    
                               employeeData.Employees = employeeResults;         
                     }

                     return Json();
                 }
4

1 回答 1

1

如果您想更新与员工对应的数据库中的数据,请检查您编辑的代码下方:

     [HttpPost]
     public JsonResult EditEmployee(Models.Employee employee) 
     {
         try
         {
             if (ModelState.IsValid)
             {
                 using (emsCtx)
                 {
                     var employeeResults = (from q in emsCtx.Employees
                                            where q.Id == employee.Id
                                             select q
                                            ).FirstOrDefault();        
                     if(employeeResults!=null)
                     {             
                           employeeResults.Column1 = employee.Column1; 
                           employeeResults.Column2 = employee.Column2; 
                           employeeResults.Column3 = employee.Column3; 
                           employeeResults.Column4 = employee.Column4; 
                     }
                     emsCtx.SaveChanges();
                 }

                 return Json();
             }
于 2013-04-01T09:39:38.220 回答