2015-06-04 38 views
0
[HttpPost] 
    public ActionResult Dep_Save_Attachments(int in_queue_id, HttpPostedFileBase httpfile, string authorized_by, string authorized_to, string confirmed_by, string approved_by) 
    { 
     if (httpfile != null) 
     { 

      var folder = Server.MapPath("~/Attachments/Laptops/" + in_queue_id + "/"); 
      var prev_fa = db.File_Attachments.Where(x => x.inventory_table_id == in_queue_id).Where(x => x.is_active == true).ToList(); 
      var prev_hfa = db.HW_Authorization_Forms.Where(x => x.file_attachments_id == prev_fa.FirstOrDefault().file_attachments_id).Where(x => x.is_active == true).ToList(); 

      if (!Directory.Exists(folder)) 
      { 
       Directory.CreateDirectory(folder); 
      } 

      if (prev_fa.Count != 0) 
      { 
       foreach (var pf in prev_fa) 
       { 
        pf.is_active = false; 

        db.Entry(pf).State = EntityState.Modified; 
        db.SaveChanges(); ; 
       } 
      } 

      if (prev_hfa.Count != 0) 
      { 
       foreach (var hpf in prev_hfa) 
       { 
        hpf.is_active = false; 

        db.Entry(hpf).State = EntityState.Modified; 
        db.SaveChanges(); ; 
       } 
      } 



      try 
      { 
       string path = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/Laptops/" + in_queue_id + "/") + System.IO.Path.GetFileName(httpfile.FileName); 
       httpfile.SaveAs(path); 

       File_Attachments fa = new File_Attachments(); 

       fa.file_attachments_id = 1; 
       fa.inventory_table_name = "Laptops_Transactions"; 
       fa.inventory_table_id = in_queue_id; 
       fa.file_name = System.IO.Path.GetFileName(httpfile.FileName); 
       fa.file_path = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Attachments/Laptops/" + httpfile.FileName; 
       fa.created_by = @User.Identity.Name.Remove(0, 9).ToLower(); 
       fa.created_date = System.DateTime.Now; 
       fa.is_active = true; 

       db.File_Attachments.Add(fa); 
       db.SaveChanges(); 

       Laptops_Transactions laptops_trans = db.Laptops_Transactions.Find(in_queue_id); 
       laptops_trans.lp_trans_type = "deployed"; 
       laptops_trans.lp_date_returned = System.DateTime.Now; 

       db.Entry(laptops_trans).State = EntityState.Modified; 
       db.SaveChanges(); 

       HW_Authorization_Forms hwf = new HW_Authorization_Forms(); 
       hwf.hw_authorization_forms_id = 1; 
       hwf.file_attachments_id = fa.file_attachments_id; 
       hwf.hw_authorized_by = authorized_by; 
       hwf.hw_authorized_to = authorized_to; 
       hwf.hw_confirmed_by = confirmed_by; 
       hwf.hw_approved_by = approved_by; 
       hwf.hw_approved_date = fa.created_date; 
       hwf.created_by = fa.created_by; 
       hwf.created_date = fa.created_date; 
       hwf.hw_authorized_date = fa.created_date; 
       hwf.hw_confirmed_date = fa.created_date; 
       hwf.is_active = true; 

       db.HW_Authorization_Forms.Add(hwf); 
       db.SaveChanges(); 

      } 
      catch 
      { 

      } 
     } 
     else 
     { 

      return Content("<script language='javascript' type='text/javascript'>alert('Please Attach the Deployment Authorization Form! Kindly go back to previous page');</script>"); 
     } 

    return RedirectToAction("Index"); 

    } 

的错误是在这条线:错误:只有原始类型或枚举类型在这方面的支持

var prev_hfa = db.HW_Authorization_Forms.Where(x => x.file_attachments_id == prev_fa.FirstOrDefault().file_attachments_id).Where(x => x.is_active == true).ToList(); 

这是我在控制器代码,其实这是工作了,不过突然有一个错误。我真的不知道为什么我有这种错误之前,它完美的作品。

请帮我这个。需要一些建议。提前致谢。

回答

0

的错误是因为数据类型问题我想,你需要确认你用正确的数据类型做,数据库file_attachments_id,并从比较值必须是相同的。

此外,is_active必须是数据类型布尔值。纠正这可能会解决您的错误。

+0

所有的数据类型都很好。 – Jen143Me

+0

你确定吗? file_attachments_id是数据库和prev_fa域的非空整数? – developer

+0

在另一个表中,file_attachments_id为空 – Jen143Me

相关问题