public ActionResult Create(PengadaanObat pengadaanobat)
        {
            if (ModelState.IsValid)
            {
                /*
                int count = Int32.Parse(Request["count"]);
                for (int kep = 1; kep <= count; kep++)
                {
                    var Jumlah = Request["Jumlah" + kep + ""].ToString();
                    var ObatID = Request["ObatID" + kep + ""].ToString();
                    var HargaAktual = Request["HargaAktual" + kep + ""].ToString();
                    var HET = Request["HET" + kep + ""].ToString();
                    if (Jumlah == null)
                        Int32.Parse(Jumlah);
                    pengadaanobat.Jumlah = Int32.Parse(Jumlah);
                    pengadaanobat.ObatID = Int32.Parse(ObatID);
                    if (HargaAktual != null) { pengadaanobat.HargaAktual = Int32.Parse(HargaAktual); }
                    if (HET != null) { pengadaanobat.HET = Int32.Parse(HET); }
                    db.PengadaanObat.Add(pengadaanobat);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
                */

                int count = Int32.Parse(Request["count"]);
                ViewBag.count = count;
                var arrayJumlah = new string[100];
                var arrayObat = new string[100];
                var arrayONama = new string[100];
                int[] arrayObatIDInt = new int[100];
                var arrayAktual = new string[100];
                var arrayHET = new string[100];
                bool adaEJ = false;
                bool adaEO = false;
                string baris1 = "", baris2 = "";

                for (int kepo = 1; kepo <= count; kepo++)
                {
                    var Jumlah = Request["Jumlah" + kepo + ""];
                    if (!String.IsNullOrEmpty(Jumlah))
                    {
                        ViewBag.JumlahError = "";
                        arrayJumlah[kepo] = Jumlah;
                    }
                    else
                    {
                        arrayJumlah[kepo] = "";
                        adaEJ = true;
                        if (baris1 == "")
                            baris1 += kepo.ToString();
                        else
                            baris1 += ", " + kepo.ToString();
                    }

                    var ObatID = Request["ObatID" + kepo + ""];
                    if (!String.IsNullOrEmpty(ObatID) && ObatID != "--Pilih Obat--")
                    {
                        //@ViewBag.JumlahError = "";
                        string[] words = ObatID.Split('&');
                        arrayObatIDInt[kepo] = int.Parse(words[0]);
                        //ViewBag.word = ObatID;
                        arrayONama[kepo] = words[1];
                        arrayObat[kepo] = ObatID;
                    }
                    else
                    {
                        //ViewBag.JumlahError = "Form nama obat tidak boleh kosong";
                        arrayObat[kepo] = "";
                        adaEO = true;
                        if (baris2 == "")
                            baris2 += kepo.ToString();
                        else
                            baris2 += ", " + kepo.ToString();
                    }

                    var HargaAktual = Request["HargaAktual" + kepo + ""];
                    if (!String.IsNullOrEmpty(HargaAktual))
                    {
                        //@ViewBag.JumlahError = "";
                        arrayAktual[kepo] = HargaAktual;
                    }
                    else
                    {
                        //ViewBag.JumlahError = "Form nama obat tidak boleh kosong";
                        arrayAktual[kepo] = "";
                    }

                    var HET = Request["HET" + kepo + ""];
                    if (!String.IsNullOrEmpty(HET))
                    {
                        //@ViewBag.JumlahError = "";
                        arrayHET[kepo] = HET;
                    }
                    else
                    {
                        //ViewBag.JumlahError = "Form nama obat tidak boleh kosong";
                        arrayHET[kepo] = "";
                    }

                    //if (!String.IsNullOrEmpty(HargaAktual) || !String.IsNullOrEmpty(HET))
                    //{
                    // pengadaanobat.HargaAktual = Int32.Parse(HargaAktual);
                    // pengadaanobat.HET = Int32.Parse(HET);
                    //}
                }

                if (adaEJ) ViewBag.JumlahError = "Jumlah obat harus diisi pada baris " + baris1 + ".";
                if (adaEO) ViewBag.ObatError = "Nama obat harus diisi pada baris " + baris2 + ".";

                if (!adaEJ && !adaEO)
                {
                    for (int kepo = 1; kepo <= count; kepo++)
                    {
                        pengadaanobat.Jumlah = Int32.Parse(arrayJumlah[kepo]);
                        pengadaanobat.ObatID = arrayObatIDInt[kepo];
                        if (arrayAktual[kepo] != "") { pengadaanobat.HargaAktual = Int32.Parse(arrayAktual[kepo]); }
                        if (arrayHET[kepo] != "") { pengadaanobat.HET = Int32.Parse(arrayHET[kepo]); }
                        db.PengadaanObat.Add(pengadaanobat);
                        db.SaveChanges();
                    }
                    return RedirectToAction("Index");
                }
                ViewBag.Jumlah = arrayJumlah;
                ViewBag.ONama = arrayONama;
                ViewBag.Obat = arrayObat;
                ViewBag.Aktual = arrayAktual;
                ViewBag.HET = arrayHET;
                ViewBag.PenyediaObatID = new SelectList(db.PenyediaObat, "ID", "Nama", pengadaanobat.PenyediaObatID);
                return View(pengadaanobat);
            }
            ViewBag.PenyediaObatID = new SelectList(db.PenyediaObat, "ID", "Nama", pengadaanobat.PenyediaObatID);
            ViewBag.ObatID = new SelectList(db.Obat, "ID", "Nama", pengadaanobat.ObatID);
            return View(pengadaanobat);
        }
        public ActionResult EditPost(int? id, PengadaanObat pengadaanobat, string jmlsblmedit)
        {
            ViewBag.E = false;

            //tanggal tidak boleh kosong
            if (pengadaanobat.Tanggal == null)
            {
                ViewBag.E = true;
                ViewBag.E1 = true;
            }

            //jumlah obat tidak boleh kosong
            if (pengadaanobat.Jumlah == null)
            {
                ViewBag.E = true;
                ViewBag.E2 = true;
            }

            //jumlah obat harus bilangan positif
            string jumlah = pengadaanobat.Jumlah.ToString();
            if (pengadaanobat.Jumlah != null)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(jumlah, "^[0-9]+$"))
                {
                    ViewBag.E = true;
                    ViewBag.E3 = true;
                }
            }

            //jumlah obat jika stoksaatedit=stoksblmedit-jmlsblmedit bernilai negatif
            var stoksblmedit = (from i in db.StokObat
                                where i.ObatID == pengadaanobat.ObatID
                                where i.KlinikID == 2
                                select i.Stok).FirstOrDefault().ToString();
            var intjmlsblmedit = int.Parse(jmlsblmedit);
            var intstoksblmedit = int.Parse(stoksblmedit);
            var stoksaatedit = intstoksblmedit - intjmlsblmedit;
            if (pengadaanobat.Jumlah != null)
            {
                var jmlsaatedit = pengadaanobat.Jumlah.ToString();
                var intjmlsaatedit = int.Parse(jmlsaatedit);

                if (stoksaatedit < 0 && intjmlsaatedit < Math.Abs(stoksaatedit))
                {
                    ViewBag.E = true;
                    ViewBag.E4 = true;
                    ViewBag.stoksaatedit = Math.Abs(stoksaatedit);
                }
            }

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var pengadaanupdate = db.PengadaanObat.Where(i => i.ID == id).Single();

            if (TryUpdateModel(pengadaanupdate, "",
                new string[] { "ID", "ObatID", "Jumlah", "Tanggal", "HET", "HargaAktual", "PenyediaObatID" }) && !ViewBag.E)
            {
                try
                {
                    db.Entry(pengadaanupdate).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (RetryLimitExceededException /* dex*/)
                {
                    //log the error, uncommend dex and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try Again, and if the problem persists, see your system administrator.");
                }
            }
            //kalau gagal balik ke action edit
            return RedirectToAction("Edit", new { E = ViewBag.E, E1 = ViewBag.E1, E2 = ViewBag.E2, E3 = ViewBag.E3, E4 = ViewBag.E4, S = ViewBag.stoksaatedit });
        }