コード例 #1
0
ファイル: HomeController.cs プロジェクト: Slightom/Classified
        public PartialViewResult GenerateAllAttributes(int?id)
        {
            AllAtributesModel aam = new AllAtributesModel();

            aam.Engine_power     = 100;
            aam.Enginie_capacity = 1.9;
            aam.Mileage          = 1000000;
            aam.Number_of_pages  = 100;
            aam.Year             = 2017;
            aam.Publication_Year = 2017;


            ViewBag.fuel              = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 1), "Value", "Value");
            ViewBag.Body_type         = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 4), "Value", "Value");
            ViewBag.Transmission      = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 5), "Value", "Value");
            ViewBag.Country_of_origin = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 6), "Value", "Value");
            ViewBag.Color             = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 9), "Value", "Value");


            if (id != null)
            {
                Classified c    = db.Classifieds.Find(id);
                var        list = giveMeSelectedAttributes2(c.Category.Name);

                foreach (string s in list)
                {
                    string ss = whiteSpacesAgain(s);
                    ClassifiedAttribute ca = db.ClassifiedAttributes.Where(p => p.ClassifiedID == c.ClassifiedID && p.Attribute.Name == ss).First();

                    switch (s)
                    {
                    case "fuel": aam.fuel = ca.Value; ViewBag.fuel = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 1), "Value", "Value", aam.fuel);  break;

                    case "Engine_power": aam.Engine_power = Double.Parse(ca.Value); break;

                    case "Engine_capacity": aam.Enginie_capacity = Double.Parse(ca.Value); break;

                    case "Body_type": aam.Body_type = ca.Value; ViewBag.Body_type = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 4), "Value", "Value", aam.Body_type); break;

                    case "Transmission": aam.Transmission = ca.Value; ViewBag.Transmission = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 5), "Value", "Value", aam.Transmission); break;

                    case "Country_of_origin": aam.Country_of_origin = ca.Value; ViewBag.Country_of_origin = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 6), "Value", "Value", aam.Country_of_origin); break;

                    case "Mileage": aam.Mileage = Double.Parse(ca.Value); break;

                    case "Year": aam.Year = Int32.Parse(ca.Value); break;

                    case "Color": aam.Color = ca.Value; ViewBag.Color = new SelectList(db.AttributeValues.Where(p => p.AttributeID == 9), "Value", "Value", aam.Color); break;

                    case "Number_of_pages": aam.Number_of_pages = Int32.Parse(ca.Value); break;

                    case "Publication_Year": aam.Publication_Year = Int32.Parse(ca.Value); break;
                    }
                }

                ViewBag.edit = "true";
            }

            return(PartialView("GenerateAllAttributes", aam));
        }
コード例 #2
0
        public ActionResult Edit([Bind(Include = "ClassifiedID,UserID,Name,Description,CategoryID,DateAdded,Price,State,Counter,Reported")] Classified classified, AllAtributesModel aam, Location l, HttpPostedFileBase upload0, HttpPostedFileBase upload1, HttpPostedFileBase upload2, HttpPostedFileBase upload3)
        {
            if (ModelState.IsValid)
            {
                ViewBag.badWord = consistBadWord(classified.Description);
                if (ViewBag.badWord != "")
                {
                    return(View("BadWord"));
                }

                string pom = db.Categories.Where(p => p.CategoryID == classified.CategoryID).Select(p => p.Name).First().ToString();
                classified.CategoryPath = categoryPath(pom);

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



                ClassifiedLocation cl  = db.ClassifiedLocations.Where(p => p.ClassifiedID == classified.ClassifiedID).First();
                string             lid = db.Locations.Where(p => p.LocationName == l.LocationName).Select(p => p.LocationID).First().ToString();
                cl.LocationID = Int32.Parse(lid);
                db.SaveChanges();



                if (upload0 != null)
                {
                    //remove old
                    var    old       = findOld(classified.ClassifiedID, "upload0");
                    var    photoName = old.Path;
                    string fullPath  = Request.MapPath(photoName);

                    if (!old.Path.Equals("default.jpg"))
                    {
                        System.IO.File.Delete(fullPath);

                        db.Photos.Remove(old);
                        db.SaveChanges();
                        //add new

                        Photo p = new Photo();
                        p.ClassifiedID = classified.ClassifiedID;

                        //pelny sciezka do pliku
                        var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload0.jpg");
                        //zapis
                        upload0.SaveAs(path);

                        p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload0.jpg";
                        p.MainPhoto = true;

                        db.Photos.Add(p);
                        db.SaveChanges();
                    }


                    else
                    {
                        //add new

                        //pelny sciezka do pliku
                        var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload0.jpg");
                        //zapis
                        upload0.SaveAs(path);

                        old.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload0.jpg";
                        old.MainPhoto = true;

                        db.SaveChanges();
                    }
                }
                else
                {
                }

                if (upload1 != null)
                {
                    //remove old
                    var old = findOld(classified.ClassifiedID, "upload1");
                    if (old != null)
                    {
                        var    photoName = old.Path;
                        string fullPath  = Request.MapPath(photoName);

                        System.IO.File.Delete(fullPath);

                        db.Photos.Remove(old);
                        db.SaveChanges();

                        //add new
                    }

                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;
                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload1.jpg");
                    //zapis
                    upload1.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload1.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                if (upload2 != null)
                {
                    //remove old
                    var old = findOld(classified.ClassifiedID, "upload2");
                    if (old != null)
                    {
                        var    photoName = old.Path;
                        string fullPath  = Request.MapPath(photoName);

                        System.IO.File.Delete(fullPath);

                        db.Photos.Remove(old);
                        db.SaveChanges();

                        //add new
                    }


                    //add new

                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload2.jpg");
                    //zapis
                    upload2.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload2.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                if (upload3 != null)
                {
                    //remove old
                    var old = findOld(classified.ClassifiedID, "upload3");
                    if (old != null)
                    {
                        var    photoName = old.Path;
                        string fullPath  = Request.MapPath(photoName);

                        System.IO.File.Delete(fullPath);

                        db.Photos.Remove(old);
                        db.SaveChanges();

                        //add new
                    }


                    //add new
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload3.jpg");
                    //zapis
                    upload3.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload3.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }



                var list2 = db.ClassifiedAttributes.Where(p => p.ClassifiedID == classified.ClassifiedID).ToList();
                foreach (var i in list2)
                {
                    db.ClassifiedAttributes.Remove(i);
                }
                db.SaveChanges();

                var list = giveMeSelectedAttributes(classified.CategoryID);
                foreach (string s in list)
                {
                    ClassifiedAttribute ca = new ClassifiedAttribute();
                    ca.ClassifiedID = classified.ClassifiedID;
                    string ss  = HomeController.whiteSpacesAgain(s);
                    string aid = db.Attributes.Where(p => p.Name == ss).Select(p => p.AttributeID).First().ToString();
                    ca.AttributeID = Int32.Parse(aid);
                    switch (s)
                    {
                    case "fuel": ca.Value = aam.fuel; break;

                    case "Engine_power": ca.Value = aam.Engine_power.ToString(); break;

                    case "Engine_capacity": ca.Value = aam.Enginie_capacity.ToString(); break;

                    case "Body_type": ca.Value = aam.Body_type; break;

                    case "Transmission": ca.Value = aam.Transmission; break;

                    case "Country_of_origin": ca.Value = aam.Country_of_origin; break;

                    case "Mileage": ca.Value = aam.Mileage.ToString(); break;

                    case "Year": ca.Value = aam.Year.ToString(); break;

                    case "Color": ca.Value = aam.Color; break;

                    case "Number_of_pages": ca.Value = aam.Number_of_pages.ToString(); break;

                    case "Publication_Year": ca.Value = aam.Publication_Year.ToString(); break;
                    }

                    db.ClassifiedAttributes.Add(ca);
                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", classified.CategoryID);
            ViewBag.UserID     = new SelectList(db.Users, "Id", "Email", classified.UserID);
            return(View(classified));
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "ClassifiedID,UserID,Name,Description,CategoryID,DateAdded,Price,State,Counter,Reported")] Classified classified, AllAtributesModel aam, Location l, HttpPostedFileBase upload0, HttpPostedFileBase upload1, HttpPostedFileBase upload2, HttpPostedFileBase upload3)
        {
            if (ModelState.IsValid && classified.CategoryID != 0)
            {
                classified.DateAdded = DateTime.Now;
                classified.UserID    = User.Identity.GetUserId();

                ViewBag.badWord = consistBadWord(classified.Description);
                if (ViewBag.badWord != "")
                {
                    return(View("BadWord"));
                }

                string pom = db.Categories.Where(p => p.CategoryID == classified.CategoryID).Select(p => p.Name).First().ToString();
                classified.CategoryPath = categoryPath(pom);

                db.Classifieds.Add(classified);
                db.SaveChanges();

                #region adding classifiedLocation

                ClassifiedLocation cl = new ClassifiedLocation();
                cl.ClassifiedID = classified.ClassifiedID;
                string lid = db.Locations.Where(p => p.LocationName == l.LocationName).Select(p => p.LocationID).First().ToString();
                cl.LocationID = Int32.Parse(lid);
                db.ClassifiedLocations.Add(cl);
                db.SaveChanges();

                #endregion

                #region adding photos

                if (upload0 != null)
                {
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload0.jpg");
                    //zapis
                    upload0.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload0.jpg";
                    p.MainPhoto = true;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }
                else
                {
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    p.Path      = "/Content/photos/default.jpg";
                    p.MainPhoto = true;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                if (upload1 != null)
                {
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;
                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload1.jpg");
                    //zapis
                    upload1.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload1.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                if (upload2 != null)
                {
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload2.jpg");
                    //zapis
                    upload2.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload2.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                if (upload3 != null)
                {
                    Photo p = new Photo();
                    p.ClassifiedID = classified.ClassifiedID;

                    //pelny sciezka do pliku
                    var path = Path.Combine(Server.MapPath("~/Content/photos"), classified.ClassifiedID.ToString() + "_upload3.jpg");
                    //zapis
                    upload3.SaveAs(path);

                    p.Path      = "/Content/photos/" + classified.ClassifiedID.ToString() + "_upload3.jpg";
                    p.MainPhoto = false;

                    db.Photos.Add(p);
                    db.SaveChanges();
                }

                #endregion

                #region adding classifiedAttributes

                var list = giveMeSelectedAttributes(classified.CategoryID);
                foreach (string s in list)
                {
                    ClassifiedAttribute ca = new ClassifiedAttribute();
                    ca.ClassifiedID = classified.ClassifiedID;
                    string ss  = HomeController.whiteSpacesAgain(s);
                    string aid = db.Attributes.Where(p => p.Name == ss).Select(p => p.AttributeID).First().ToString();
                    ca.AttributeID = Int32.Parse(aid);
                    switch (s)
                    {
                    case "fuel": ca.Value = aam.fuel; break;

                    case "Engine_power": ca.Value = aam.Engine_power.ToString(); break;

                    case "Engine_capacity": ca.Value = aam.Enginie_capacity.ToString(); break;

                    case "Body_type": ca.Value = aam.Body_type; break;

                    case "Transmission": ca.Value = aam.Transmission; break;

                    case "Country_of_origin": ca.Value = aam.Country_of_origin; break;

                    case "Mileage": ca.Value = aam.Mileage.ToString(); break;

                    case "Year": ca.Value = aam.Year.ToString(); break;

                    case "Color": ca.Value = aam.Color; break;

                    case "Number_of_pages": ca.Value = aam.Number_of_pages.ToString(); break;

                    case "Publication_Year": ca.Value = aam.Publication_Year.ToString(); break;
                    }

                    db.ClassifiedAttributes.Add(ca);
                    db.SaveChanges();
                }



                #endregion



                return(RedirectToAction("Index"));
            }


            if (classified.CategoryID == 0)
            {
                ViewBag.Category0 = "You didn't choose Category!";
            }


            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", classified.CategoryID);
            ViewBag.UserID     = new SelectList(db.Users, "Id", "Email", classified.UserID);
            IEnumerable <SelectListItem> lss;
            List <SelectListItem>        ls = new List <SelectListItem>
            {
                new SelectListItem()
                {
                    Text = "New", Value = "1", Selected = true
                },
                new SelectListItem()
                {
                    Text = "Used", Value = "2"
                }
            };
            lss           = ls;
            ViewBag.State = new SelectList(lss, "Text", "Text");

            return(View(classified));
        }