Exemple #1
0
        public ActionResult Create(PointViewModel point)
        {
            if (ModelState.IsValid)
            {
                PointModel model = new PointModel
                {
                    Id          = point.Id != Guid.Empty ? point.Id : Guid.NewGuid(),
                    Name        = point.Name,
                    Address     = point.Address,
                    PostalCode  = point.PostalCode,
                    City        = point.City,
                    Coordenate  = point.Coordenate,
                    PhoneNumber = point.PhoneNumber,
                    URL         = point.URL,
                    SourceURL   = point.SourceURL,
                    IsActive    = point.IsActive
                };

                if (point.SelectedTopicId != null && point.SelectedTopicId.Length > 0)
                {
                    model.Topics = new List <TopicModel>();
                    for (int i = 0; i < point.SelectedTopicId.Length; i++)
                    {
                        TopicModel temp = new TopicService().GetTopic(new Guid(point.SelectedTopicId[i].ToString()));
                        model.Topics.Add(temp);
                    }
                }

                #region Validate Model After Conversion from ViewModel
                try
                {
                    if (model.IsValid)
                    {
                        service.InsertPoint(model);
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError("", model.Error);
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                }
                #endregion
            }

            ViewBag.Topics = new MultiSelectList(new TopicService().GetActiveTopics(), "Id", "Name");
            ViewBag.City   = new SelectList(new CityService().GetCities(), "Id", "Name");

            return(View(point));
        }