public ActionResult AddEditCategory(CategoryModel mod)
        {
            Category objCategory;

            if (ModelState.IsValid)
            {
                if (mod.categoryId > 0)
                {
                    objCategory = db.Categories.Where(o => o.categoryId == mod.categoryId).SingleOrDefault();
                    if (objCategory == null)
                    {
                        throw new HttpException(404, "Rate does not exists");
                    }
                }
                else
                {
                    objCategory = new Category();
                }

                objCategory.categoryTitle = mod.CategoryTilte;
                objCategory.categoryCode  = mod.CategoryCode;
                objCategory.companyid     = ClientSession.CompanyID;
                objCategory.storeid       = mod.storeid;

                objCategory.printOrder      = mod.printOrder;
                objCategory.appliesDiscount = mod.appliesDiscount;
                objCategory.active          = mod.isActive;
                objCategory.parentId        = mod.SelectedParentCategoryId;
                objCategory.storeid         = mod.SelectedStoreId;

                if (mod.categoryId > 0)
                {
                    objCategory.updaedBy        = 1;
                    objCategory.updatedDate     = DateTime.Now;
                    db.Entry(objCategory).State = EntityState.Modified;

                    db.SaveChanges();
                }
                else
                {
                    objCategory.createdBy   = 1233;
                    objCategory.createdDate = DateTime.Now;
                    db.Categories.Add(objCategory);
                }

                db.SaveChanges();

                //Users/LoadDataForDataTable
                return(RedirectToAction("ManageCategory"));
            }



            #region loadstores
            List <Store> stores = (from data in db.Stores
                                   where data.companyId == ClientSession.CompanyID
                                   select data).ToList();

            Store objStores = new Store();
            objStores.storeName = "All Stores";
            objStores.storeID   = 0;
            stores.Insert(0, objStores);
            mod.Stores = stores.Select(a => new SelectListItem
            {
                Text  = a.storeName,
                Value = a.storeID.ToString()
            });
            #endregion

            #region loadcategories
            List <POSEntity.Category> categories = (from data in db.Categories
                                                    where data.companyid == ClientSession.CompanyID &&
                                                    data.categoryId != mod.categoryId
                                                    select data).ToList();

            POSEntity.Category objcategoryType = new POSEntity.Category();
            objcategoryType.categoryTitle = "Main Category";
            objcategoryType.categoryId    = 0;
            categories.Insert(0, objcategoryType);
            mod.Categories = categories.Select(a => new SelectListItem
            {
                Text  = a.categoryTitle,
                Value = a.categoryId.ToString()
            });
            #endregion


            return(View(mod));
        }
        public ActionResult AddEditCategory(Int64 id = 0)
        {
            int _companyid = ClientSession.CompanyID;

            CategoryModel mod = new CategoryModel();

            #region loadstores
            List <Store> stores = (from data in db.Stores
                                   where data.companyId == ClientSession.CompanyID
                                   select data).ToList();

            Store objStores = new Store();
            objStores.storeName = "All Stores";
            objStores.storeID   = 0;
            stores.Insert(0, objStores);
            mod.Stores = stores.Select(a => new SelectListItem
            {
                Text  = a.storeName,
                Value = a.storeID.ToString()
            });
            #endregion

            #region loadcategories
            List <POSEntity.Category> categories = (from data in db.Categories where data.companyid == ClientSession.CompanyID && data.categoryId != id
                                                    select data).ToList();

            POSEntity.Category objcategoryType = new POSEntity.Category();
            objcategoryType.categoryTitle = "Main Category";
            objcategoryType.categoryId    = 0;
            categories.Insert(0, objcategoryType);
            mod.Categories = categories.Select(a => new SelectListItem
            {
                Text  = a.categoryTitle,
                Value = a.categoryId.ToString()
            });
            #endregion

            Category objcategory;
            Int64    _hdnrateId = Convert.ToInt64(id);
            if (_hdnrateId > 0)
            {
                objcategory = db.Categories.Where(o => o.categoryId == _hdnrateId && o.companyid == ClientSession.CompanyID).SingleOrDefault();
                if (objcategory != null)
                {
                    mod.categoryId               = _hdnrateId;
                    mod.CategoryTilte            = objcategory.categoryTitle;
                    mod.SelectedStoreId          = objcategory.storeid.Value;
                    mod.companyid                = objcategory.companyid.Value;
                    mod.CategoryCode             = objcategory.categoryCode;
                    mod.appliesDiscount          = objcategory.appliesDiscount.Value;
                    mod.printOrder               = objcategory.printOrder.Value;
                    mod.isActive                 = objcategory.active.Value;
                    mod.SelectedParentCategoryId = objcategory.parentId.Value;
                }
                else
                {
                    throw new HttpException(404, "Rate does not exists");
                }
            }
            else
            {
                mod.SelectedParentCategoryId = 0;
            }

            return(View(mod));
        }