public ActionResult UpdateMobileAccessoriesAd([Bind(Include = "Id,category,postedBy,title,description,time")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                if (Request.IsAuthenticated)
                {
                    if (Request["postedBy"] == User.Identity.GetUserId())
                    {
                        FileName[] fileNames = JsonConvert.DeserializeObject<FileName[]>(Request["files"]);
                        electronicController.MyAd(ref ad, "Update");

                        MobileAd mobileAd = new MobileAd();

                        mobileAd.sims = Request["sims"];
                        mobileAd.color = Request["color"];
                        mobileAd.mobileId = SaveMobileBrandModel(ref ad);

                        db.Entry(ad).State = EntityState.Modified;
                        db.SaveChanges();
                        //tags
                        electronicController.SaveTags(Request["tags"], ref ad, "update");
                        //location
                        electronicController.PostAdByCompanyPage(ad.Id, true);
                       electronicController.MyAdLocation(Request["city"], Request["popularPlace"], Request["exectLocation"], ref ad, "Update");
                        electronicController.ReplaceAdImages(ref ad, fileNames);

                        
                        
                        //db.Ads.Add(ad);
                        mobileAd.Id = ad.Id;
                        db.Entry(mobileAd).State = EntityState.Modified;

                        db.SaveChanges();
                        return RedirectToAction("Details", "Electronics", new { id = ad.Id, title = ElectronicsController.URLFriendly(ad.title) });
                    }

                }
                return RedirectToAction("Register", "Account");
            }
            return View("EditAd", ad);
        }
        public ActionResult Create([Bind(Include = "Id,category,postedBy,title,description,time")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                if (Request.IsAuthenticated)
                {
                    //string tempId = Request["tempId"];
                    FileName[] fileNames = JsonConvert.DeserializeObject<FileName[]>(Request["files"]);
                    electronicController.MyAd(ref ad, "Save", "Mobiles");
                    db.Ads.Add(ad);
                    db.SaveChanges();


                    electronicController.PostAdByCompanyPage(ad.Id);

                    MobileAd mobileAd = new MobileAd();
                    mobileAd.sims = Request["sims"];
                    mobileAd.color = Request["color"];

                    mobileAd.mobileId =SaveMobileBrandModel(ref ad);
                    
                    //images
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        string sbs = e.ToString();
                    }
                    //tags
                    electronicController.SaveTags(Request["tags"],ref ad);
                    // FileUploadHandler(ad);
                    mobileAd.Id = ad.Id;
                    db.MobileAds.Add(mobileAd);
                    //ad.MobileAd.a(mobileAd);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        string sbs = e.ToString();
                    }
                    // ReplaceAdImages(ad.Id,tempId,fileNames);
                   electronicController.ReplaceAdImages(ref ad, fileNames);
                    //location
                   electronicController.MyAdLocation(Request["city"], Request["popularPlace"], Request["exectLocation"],ref ad, "Save");
                    db.SaveChanges();
                    return RedirectToAction("Details", "Electronics", new { id = ad.Id, title = ElectronicsController.URLFriendly(ad.title) });
                }
                return RedirectToAction("Register", "Account");
            }
            return View("Create", ad);
            //ViewBag.postedBy = new SelectList(db.AspNetUsers, "Id", "Email", ad.postedBy);
            //return View(ad);
        }
        public ActionResult Update([Bind(Include = "Id,category,postedBy,title,description,time")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                if (Request.IsAuthenticated)
                {
                    var ab = Request["postedBy"];
                    var iddd = User.Identity.GetUserId();
                    if (Request["postedBy"] == User.Identity.GetUserId())
                    {
                        FileName[] fileNames = JsonConvert.DeserializeObject<FileName[]>(Request["files"]);
                        electronicController.MyAd(ref ad, "Update");

                        MobileAd mobileAd = new MobileAd();

                        mobileAd.sims = Request["sims"];
                        mobileAd.color = Request["color"];

                       mobileAd.mobileId = SaveMobileBrandModel(ref ad);

                        //tags
                        electronicController.SaveTags(Request["tags"], ref ad, "update");
                        string brand = Request["brand"];
                        string model = Request["model"];
                        var mobileModel = db.MobileModels.FirstOrDefault(x => x.Mobile.brand == brand  && x.model == model );
                        mobileAd.mobileId = mobileModel.Id;
                        //asp.Ads.Add(ad);

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

                        electronicController.PostAdByCompanyPage(ad.Id,true);
                        //db.Ads.Add(ad);
                        mobileAd.Id = ad.Id;
                        db.Entry(mobileAd).State = EntityState.Modified;
                        //ad.MobileAds.Add(mobileAd);
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            string sss = e.ToString();
                        }
                        //location
                        electronicController.MyAdLocation(Request["city"], Request["popularPlace"], Request["exectLocation"], ref ad, "Update");
                        electronicController.ReplaceAdImages(ref ad, fileNames);
                        return RedirectToAction("Details", "Electronics", new { id = ad.Id, title = ElectronicsController.URLFriendly(ad.title) });
                    }
                }
                return RedirectToAction("Register", "Account");
            }
            return View("Edit", ad);
        }
예제 #4
0
        public ActionResult Create([Bind(Include="Id,category,postedBy,title,description,time")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                if (Request.IsAuthenticated)
                {
                    MobileAd mobileAd= new MobileAd();

                    var isbiding = Request["bidingAllowed"];
                    if (isbiding == "fixedPrice")
                    {
                        var nn = Request["isNegotiable"];
                        if (nn == "on")
                        {
                            ad.isnegotiable = "y";
                        }
                        else
                        {
                            ad.isnegotiable = "n";
                        }
                    }
                    else if (isbiding == "allowBiding")
                    {
                        ad.isnegotiable = "b";
                    }
                    var condition = Request["condition"];
                    if (condition == "new")
                    {
                        mobileAd.condition = "n";
                    }
                    else if (condition == "unboxed")
                    {
                        mobileAd.condition = "b";
                    }
                    else
                    {
                        mobileAd.condition = "u";
                    }
                    mobileAd.sims =int.Parse( Request["sims"]);
                    mobileAd.color = Request["color"];
                    var pp = Request["price"];
                   pp =  pp.Replace(",", string.Empty);
                    if (pp != null && pp != "" ){
                        //if (pp.Substring(pp.Length - 1) == ",")
                        //{
                        //    pp = pp.Remove(pp.Length - 1);
                        //}
                        ad.price =int.Parse( pp);
                    }
                    var company = Request["brand"];
                    var model = Request["model"];
                    ad.time = DateTime.UtcNow;
                    ad.description = System.Web.HttpUtility.HtmlEncode(ad.description);
                    ad.postedBy = User.Identity.GetUserId();
                    AspNetUser asp = db.AspNetUsers.FirstOrDefault(x => x.Id == ad.postedBy);

                    ad.category = "Electronics";
                    //var companyName = model.Split(' ')[0];
                    var allBrands = (db.Mobiles.Select(x => x.Id)).AsEnumerable(); //getBrands
                    bool isNewBrand = true;
                    foreach (var brand in allBrands)
                    {
                        if (brand == company)
                        {
                            isNewBrand = false;
                        }
                    }
                    if (isNewBrand)
                    {
                        Mobile mob = new Mobile();
                        mob.Id = company;
                        db.Mobiles.Add(mob);
                        db.SaveChanges();

                        MobileModel mod = new MobileModel();
                        mod.model = model;
                        mod.Mobile = company;
                        db.MobileModels.Add(mod);
                        db.SaveChanges();
                        //send admin notification
                    }
                    else
                    {
                        var allModels =  db.MobileModels.Where(x => x.Mobile == company).Select(x => x.model);
                        bool isNewModel = true;
                        foreach (var myModel in allModels)
                        {
                            if (myModel == model)
                            {
                                isNewModel = false;
                            }
                        }
                        if (isNewModel)
                        {
                            MobileModel mod = new MobileModel();
                            mod.Mobile = company;
                            mod.model = model;
                            db.MobileModels.Add(mod);
                            db.SaveChanges();
                            //send admin notification with user info
                        }
                    }

                    //if (companyName == "HTC" || companyName == "Nokia" || companyName == "Samsung" || companyName == "Iphone")
                    //{
                    //    string[] newModel = model.Split(' ').Skip(1).ToArray();
                    //    model = string.Join("", newModel);
                    //}

                    var mobiledata = db.Mobiles.FirstOrDefault(x => x.Id == company && x.MobileModels.Any(xu=>xu.model.Equals(model)));
                    //if (mobiledata == null)
                    //{
                    //    Mobile mob = new Mobile();
                    //    mob.Id = companyName;
                    //    db.Mobiles.Add(mob);
                    //    MobileModel mm = new MobileModel();
                    //    mm.Mobile = companyName;
                    //    mm.model = model;
                    //    //send admin notification
                    //}
                    mobileAd.mobileId = mobiledata.Id;
                    asp.Ads.Add(ad);
                    db.Ads.Add(ad);
                    mobileAd.adId = ad.Id;
                    ad.MobileAds.Add(mobileAd);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        string s = e.ToString();
                    }
                    return RedirectToAction("Index", new {category = "mobiles",subcategory = mobiledata.Id,lowcategory = mobiledata.MobileModels.FirstOrDefault(x=>x.model.Equals(model)).model,id = ad.Id , title = ad.title });
                }
                TempData["error"] = "You must be logged in to post ad";
                return View("Create", ad);
            }
            TempData["error"] = "Only enter those information about which you are asked";
            return View("Create", ad);
            //ViewBag.postedBy = new SelectList(db.AspNetUsers, "Id", "Email", ad.postedBy);
            //return View(ad);
        }