public ActionResult DeleteExportitem(int exp_id, List <int> itemCheckedIds, List <int> countryCheckboxes)
        {
            var model = new Export_view();

            if (itemCheckedIds != null)
            {
                foreach (var itm_id in itemCheckedIds)
                {
                    Export_Item exp_item = db.Export_Item.Find(itm_id);
                    db.Export_Item.Remove(exp_item);
                    db.SaveChanges();
                }
            }
            if (countryCheckboxes != null)
            {
                foreach (var c_id in countryCheckboxes)
                {
                    Export_Country c_item = db.Export_Country.Find(c_id);
                    db.Export_Country.Remove(c_item);
                    db.SaveChanges();
                }

                var country = db.Exports.Where(x => x.Id == exp_id).Include(z => z.Export_Country.Select(y => y.Country));

                model.exp_country = country;
                //    ViewBag.t_id = exp_id.Value;

                return(PartialView("Displaycountry", model));
            }



            var items = db.Exports.Where(x => x.Id == exp_id).Include(z => z.Export_Item.Select(y => y.Item));

            model.exp_item = items;


            return(PartialView("Itemdisplay", model));
        }
        public JsonResult Additem(ViewExportAdd exportvalues)
        {
            var exp_id = exportvalues.exp_id;

            ViewExportAdd model = new ViewExportAdd();
            Export_view   view  = new Export_view();

            var lic_id = db.Exports.Where(z => z.Id == exp_id).Include(x => x.License).Single();

            if (exportvalues.Selectedcountrylist != null)
            {
                foreach (var i in exportvalues.Selectedcountrylist)
                {
                    var c_add      = new Export_Country();
                    var item_found = from itm in db.Export_Country where itm.Country_Id == i && itm.Export_Id == exp_id select itm;

                    if (item_found.Any())
                    {
                        var item_name = from itm in db.Countries where itm.Id == i select itm.Name;
                        model.msg = "Item - " + item_name.FirstOrDefault() + "item already added ";
                        view.msg  = model.msg;
                        //return RedirectToAction("GetExpDetails",new { exp_id = exp_id.Value, msg = model.msg });
                        break;
                    }


                    else
                    {
                        c_add.Country_Id = i;

                        c_add.Export_Id = exp_id;
                        db.Export_Country.Add(c_add);
                        db.SaveChanges();
                        model.msg = "country Added";
                    }
                }
            }
            if (exportvalues.SelectedItems != null)
            {
                foreach (var i in exportvalues.SelectedItems)
                {
                    var items_add   = new Export_Item();
                    var units_total = from itm in db.License_Item where itm.Item_Id == i && itm.License_Id == lic_id.License_Id select itm.No_Units;
                    var exp_ids     = db.Exports.Where(z => z.License_Id == lic_id.License_Id).Select(y => y.Id);

                    var sum_units = from f in db.Export_Item
                                    where exp_ids.Contains(f.Exporter_Id)
                                    group f by f.Item_Id into g
                                    orderby g.Sum(x => x.No_Of_Units)
                                    select
                                    new
                    {
                        item_code = g.Key,
                        //item_value = g.First().,
                        total = g.Sum(x => x.No_Of_Units)
                    };

                    var sum = 0;

                    foreach (var n in sum_units)
                    {
                        if (n.item_code == i)
                        {
                            sum = n.total;
                            break;
                        }
                    }

                    var units_allowed = (units_total.SingleOrDefault()) - sum;
                    if ((
                            units_allowed) < exportvalues.No_Of_Units)
                    {
                        model.msg = "no of units can't  exceed than" + (
                            units_allowed);
                        view.msg = model.msg;
                        break;
                        //  return RedirectToAction("GetExpDetails", new { exp_id = exp_id.Value, msg = model.msg });
                    }
                    var item_found = from itm in db.Export_Item where itm.Item_Id == i && itm.Exporter_Id == exp_id select itm;

                    if (item_found.Any())
                    {
                        item_found.First().No_Of_Units = exportvalues.No_Of_Units;
                        db.SaveChanges();
                        var item_name = from itm in db.Items where itm.Id == i select itm.Name;
                        model.msg = "Item - " + item_name.FirstOrDefault() + "item updated ";
                        view.msg  = model.msg;
                        //return RedirectToAction("GetExpDetails",new { exp_id = exp_id.Value, msg = model.msg });
                        break;
                    }


                    else
                    {
                        items_add.Item_Id     = i;
                        items_add.No_Of_Units = exportvalues.No_Of_Units;
                        items_add.Exporter_Id = exp_id;
                        db.Export_Item.Add(items_add);
                        db.SaveChanges();
                        model.msg = "item Added";
                    }
                }
            }


            model.exp_id = exp_id;
            //  model.Items= new SelectListItem(item_list.ToList(), "Id", "Name")



            // db.SaveChanges();
            // return PartialView("EdititemPartialView", model);
            // return View(model);
            //   return RedirectToAction("GetExpDetails", new { exp_id = exp_id.Value, msg = "item added" });
            return(Json(new { msg = model.msg }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult AddExportitem(int?exp_id, int?lic_id, ViewExportAdd exportvalues)
        {
            TempData["id"] = exp_id.Value;
            ViewExportAdd model = new ViewExportAdd();

            if (exp_id != null)
            {
                model.exp_item = db.Exports.Where(z => z.Id == exp_id.Value).Include(x => x.Export_Item.Select(y => y.Item));
            }
            if (exportvalues.SelectedItems != null)
            {
                foreach (var i in exportvalues.SelectedItems)
                {
                    var items_add = new Export_Item();


                    var units_total = from itm in db.License_Item where itm.Item_Id == i && itm.License_Id == lic_id.Value select itm.No_Units;
                    var exp_ids     = db.Exports.Where(z => z.License_Id == lic_id.Value).Select(y => y.Id);

                    var sum_units = from f in db.Export_Item
                                    where exp_ids.Contains(f.Exporter_Id)
                                    group f by f.Item_Id into g
                                    orderby g.Sum(x => x.No_Of_Units)
                                    select
                                    new
                    {
                        item_code = g.Key,
                        //item_value = g.First().,
                        total = g.Sum(x => x.No_Of_Units)
                    };

                    var sum = 0;

                    foreach (var n in sum_units)
                    {
                        if (n.item_code == i)
                        {
                            sum = n.total;
                            break;
                        }
                    }

                    var units_allowed = (units_total.SingleOrDefault()) - sum;
                    if ((
                            units_allowed) < exportvalues.No_Of_Units)
                    {
                        model.msg = "no of units can't  exceed than" + (
                            units_allowed);
                        //  view.msg = model.msg;
                        break;
                        //  return RedirectToAction("GetExpDetails", new { exp_id = exp_id.Value, msg = model.msg });
                    }
                    var item_found = from itm in db.Export_Item where itm.Item_Id == i && itm.Exporter_Id == exp_id select itm;

                    if (item_found.Any())
                    {
                        item_found.First().No_Of_Units = exportvalues.No_Of_Units;
                        db.SaveChanges();
                        var item_name = from itm in db.Items where itm.Id == i select itm.Name;
                        model.msg = "Item - " + item_name.FirstOrDefault() + "item updated ";
                        //   view.msg = model.msg;
                        //return RedirectToAction("GetExpDetails",new { exp_id = exp_id.Value, msg = model.msg });
                        break;
                    }
                    else
                    {
                        items_add.Item_Id     = i;
                        items_add.No_Of_Units = exportvalues.No_Of_Units;
                        items_add.Exporter_Id = exp_id.Value;
                        db.Export_Item.Add(items_add);
                        db.SaveChanges();
                    }
                }
            }
            var item_list =
                from itm in db.Items
                join lic in db.License_Item on itm.Id equals lic.Item_Id
                where lic.License_Id == lic_id.Value
                select itm;

            model.lic_id = lic_id.Value;
            model.exp_id = exp_id.Value;
            //  model.Items= new SelectListItem(item_list.ToList(), "Id", "Name")

            model.Items =
                item_list.Select(x => new SelectListItem
            {
                Value = x.Id.ToString(),
                Text  = x.Name,
            })
                .ToList();

            model.SelectedItems = db.Items.Select(x => x.Id);



            return(View(model));
        }