コード例 #1
0
 public ActionResult Index(string key)
 {
     var viewModel = new EnterpriseViewModel();
     if (!string.IsNullOrEmpty(key))
     {
         var enterprise = Db.Enterprises.GetCompleteEnterprise(EnterpriseHelper.GetId(key)).Enterprise;
         if ((enterprise.IsNew && !enterprise.LockedFromEdit) || (HttpContext.User.Identity.IsAuthenticated && CurrentAccount.IsAdmin))
         {
             viewModel = enterprise;
             viewModel.ShowForm = true;
         }
     }
     return View(viewModel);
 }
コード例 #2
0
        public ActionResult CreateTempEnterprise(EnterpriseViewModel viewModel)
        {
            if (!string.IsNullOrEmpty(viewModel.Nope))
                return RedirectToAction("Index", "Home");

            if (string.IsNullOrEmpty(viewModel.Name))
                ModelState.AddModelError("Name", "Ange restaurangens namn");

            if (viewModel.DisplayCategories == null || viewModel.DisplayCategories.Count < 1)
                ModelState.AddModelError("DisplayCategories", "Välj minst en kategori");
            else
            {
                viewModel.DisplayCategories = EnterpriseHelper.GetDisplayCategories(viewModel.DisplayCategories);
            }

            if (viewModel.Coordinates.Lat < 1 || viewModel.Coordinates.Lng < 1)
                ModelState.AddModelError("Coordinates", "Du måste ange någon platsinfo");

            if (ModelState.IsValid)
            {
                var categoryList = new List<string>();
                categoryList.AddRange(viewModel.DisplayCategories.Take(6).Select(catgory => catgory.Value));

                var enterprise = new Enterprise
                {
                    Name = viewModel.Name,
                    Phone = viewModel.Phone,
                    StreetNumber = viewModel.StreetNumber,
                    StreetRoute = viewModel.StreetRoute,
                    PostalCode = viewModel.PostalCode,
                    PostalTown = viewModel.PostalTown,
                    Commune = viewModel.Commune,
                    County = viewModel.County,
                    SubLocality = viewModel.SubLocality,
                    CountryCode = viewModel.CountryCode ?? "SE",

                    SearchTags = EnterpriseHelper.GenerateSearchTags(viewModel.Name),

                    Coordinates = new Coordinates { Lat = viewModel.Coordinates.Lat, Lng = viewModel.Coordinates.Lng },
                    Categories = categoryList,

                    IsNew = true,
                    OwnedByAccount = false,
                    LockedFromEdit = false,

                    LastUpdated = DateTime.Now,

                    Menu = new Menu()
                };

                if (string.IsNullOrEmpty(viewModel.Id))
                {
                    enterprise.Id = EnterpriseHelper.GetId(GeneralHelper.GetGuid());
                    Db.Enterprises.CreateEnterprise(enterprise);
                }
                else
                {
                    var enterpriseInDb = Db.Enterprises.GetEnterpriseById(enterprise.Id);
                    if (enterpriseInDb != null)
                    {
                        Db.Enterprises.UpdateEnterprise(enterprise);
                    }
                }
                return Json(new { url =  Url.Action("Edit", new { key = EnterpriseHelper.GetKey(enterprise.Id) }) });
            }

            viewModel.ShowForm = true;

            return PartialView("~/Views/Manage/_AddEnterpriseForm.cshtml", viewModel);
        }