コード例 #1
0
        public ActionResult Products(ProductModel model, HttpPostedFileBase image)
        {
            if (User.Identity.IsAuthenticated)
            {
                var scope = ObjectScopeProvider1.GetNewObjectScope();
                if (Checkauthorization(scope, User.Identity.Name))
                {
                    model.CategoryName = (from c in scope.GetOqlQuery<Category>().ExecuteEnumerable()
                                          where c.Deletedstatus == DeleteStatus.Working
                                          select c).Select(prod =>
                      new SelectListItem
                      {
                          Selected = false,
                          Text = prod.Name,
                          Value = prod.Id.ToString()
                      });

                    if (!string.IsNullOrEmpty(model.ProductName) && !string.IsNullOrEmpty(model.ProductDescription) && !string.IsNullOrEmpty(model.ProductPrice) && image != null && image.ContentLength != 0)
                    {
                        Guid categoryId;
                        try
                        {
                            categoryId = new Guid(Request.Form["CategoryName"]);
                        }
                        catch (Exception)
                        {
                            categoryId = Guid.Empty;
                        }
                        if (categoryId != Guid.Empty)
                        {
                            var categories = (from c in scope.GetOqlQuery<Category>().ExecuteEnumerable()
                                              where c.Id.Equals(categoryId)
                                              select c).ToList();
                            if (categories.Count > 0)
                            {
                                bool exitFlag = false;
                                foreach (var category in categories)
                                {
                                    if (!exitFlag)
                                    {
                                        foreach (var selectListItem in model.CategoryName)
                                        {
                                            if (selectListItem.Value == category.Id.ToString())
                                            {
                                                selectListItem.Selected = true;
                                                exitFlag = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                double price;
                                try
                                {
                                    price = Convert.ToDouble(model.ProductPrice);
                                }
                                catch (Exception)
                                {
                                    price = 0.0;
                                }
                                if (price != 0.0)
                                {
                                    scope.Transaction.Begin();
                                    var product = new Product
                                                      {
                                                          Category = categories[0].Name,
                                                          Name = model.ProductName,
                                                          Id = Guid.NewGuid(),
                                                          Price = price,
                                                          Description = model.ProductDescription
                                                      };
                                    // saving image here
                                    try
                                    {
                                        var productFile = new File { Filename = image.FileName };
                                        Stream fileStream = image.InputStream;
                                        int fileLength = image.ContentLength;
                                        productFile.Filedata = new byte[fileLength];
                                        fileStream.Read(productFile.Filedata, 0, fileLength);
                                        productFile.MimeType = image.ContentType;
                                        productFile.ID = Guid.NewGuid();
                                        product.Productfile = productFile;
                                    }
                                    catch
                                    { }
                                    categories[0].Products.Add(product);
                                    scope.Add(categories[0]);
                                    scope.Transaction.Commit();
                                    return RedirectToAction("Products");
                                }
                                ModelState.Remove("CategoryName");
                                if (ModelState.IsValidField("ProductPrice"))
                                    ModelState.AddModelError("ProductPrice", "Input proper price value.");
                                return View(model);
                            }
                        }
                        ModelState.Remove("CategoryName");
                        return View(model);
                    }
                    ModelState.Remove("CategoryName");
                    return View(model);
                }
                ViewData["Status"] = "You are not authorized to do this operation";
                return View("Status");
            }
            return RedirectToAction("LogOn");
        }
コード例 #2
0
 public ActionResult Editproduct(ProductModel model, HttpPostedFileBase image)
 {
     if (User.Identity.IsAuthenticated)
     {
         var scope = ObjectScopeProvider1.GetNewObjectScope();
         if (Checkauthorization(scope, User.Identity.Name))
         {
             if (!string.IsNullOrEmpty(model.ProductName) && !string.IsNullOrEmpty(model.ProductDescription) && !string.IsNullOrEmpty(model.ProductPrice))
             {
                 double price;
                 try
                 {
                     price = Convert.ToDouble(model.ProductPrice);
                 }
                 catch (Exception)
                 {
                     price = 0.0;
                 }
                 if (price != 0.0)
                 {
                     List<Product> products = (from c in scope.GetOqlQuery<Product>().ExecuteEnumerable()
                                               where c.Id.ToString().Equals(model.Id)
                                               select c).ToList();
                     if (products.Count > 0)
                     {
                         scope.Transaction.Begin();
                         products[0].Name = model.ProductName;
                         products[0].Description = model.ProductDescription;
                         products[0].Price = price;
                         if (image != null && image.ContentLength != 0)
                         {
                             // updating image here
                             try
                             {
                                 products[0].Productfile.Filename = image.FileName;
                                 Stream fileStream = image.InputStream;
                                 int fileLength = image.ContentLength;
                                 products[0].Productfile.Filedata = new byte[fileLength];
                                 fileStream.Read(products[0].Productfile.Filedata, 0, fileLength);
                                 products[0].Productfile.MimeType = image.ContentType;
                             }
                             catch
                             {
                             }
                         }
                         scope.Add(products[0]);
                         scope.Transaction.Commit();
                         ViewData["Status"] = "Product updated successfully. <br /> now you can close this window.";
                         return View("success_small");
                     }
                     ModelState.Remove("CategoryName");
                     return View(model);
                 }
                 ModelState.Remove("CategoryName");
                 if (ModelState.IsValidField("ProductPrice"))
                     ModelState.AddModelError("ProductPrice", "Input proper price value.");
                 return View(model);
             }
             ModelState.Remove("CategoryName");
             return View(model);
         }
         ViewData["Status"] = "You are not authorized to do this operation";
         return View("success");
     }
     return RedirectToAction("LogOn");
 }
コード例 #3
0
 public ActionResult Products()
 {
     if (User.Identity.IsAuthenticated)
     {
         var scope = ObjectScopeProvider1.GetNewObjectScope();
         if (Checkauthorization(scope, User.Identity.Name))
         {
             var productModel = new ProductModel();
             productModel.CategoryName = (from c in scope.GetOqlQuery<Category>().ExecuteEnumerable()
                                          where c.Deletedstatus == DeleteStatus.Working
                                          select c).Select(prod =>
               new SelectListItem
               {
                   Selected = false,
                   Text = prod.Name,
                   Value = prod.Id.ToString()
               });
             return View(productModel);
         }
         ViewData["Status"] = "You are not authorized to do this operation";
         return View("Status");
     }
     return RedirectToAction("LogOn");
 }
コード例 #4
0
 public ActionResult Editproduct(string id)
 {
     if (User.Identity.IsAuthenticated)
     {
         Guid productId;
         try
         {
             productId = new Guid(id);
         }
         catch (Exception)
         {
             productId = Guid.Empty;
         }
         if (productId != Guid.Empty)
         {
             var scope = ObjectScopeProvider1.GetNewObjectScope();
             if (Checkauthorization(scope, User.Identity.Name))
             {
                 List<Product> products = (from c in scope.GetOqlQuery<Product>().ExecuteEnumerable()
                                           where c.Id.Equals(productId)
                                           select c).ToList();
                 if (products.Count > 0)
                 {
                     var productModel = new ProductModel();
                     productModel.ProductDescription = products[0].Description;
                     productModel.ProductName = products[0].Name;
                     productModel.ProductPrice = products[0].Price.ToString();
                     productModel.Id = products[0].Id.ToString();
                     productModel.CategoryName = (from c in scope.GetOqlQuery<Category>().ExecuteEnumerable()
                                                  select c).Select(prod =>
               new SelectListItem
               {
                   Selected = false,
                   Text = prod.Name,
                   Value = prod.Id.ToString()
               });
                     foreach (var selectListItem in productModel.CategoryName)
                     {
                         if (selectListItem.Text.ToLower() == products[0].Category.ToLower())
                         {
                             selectListItem.Selected = true;
                             break;
                         }
                     }
                     return View(productModel);
                 }
             }
             ViewData["Status"] = "You are not authorized to do this operation";
             return View("Status");
         }
         return RedirectToAction("Home");
     }
     return RedirectToAction("LogOn");
 }