示例#1
0
        // PUT: api/Hero/5
        public HttpResponseMessage Put([FromBody] HeroViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError("Invalid Hero")));
            }

            try
            {
                var validation = _heroAppService.Update(model);
                if (!validation.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError(validation.Erros.ToJson())));
                }

                return(Request.CreateResponse(HttpStatusCode.Created, validation.ToJson()));
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
        public ActionResult Edit(Storm.Business.ViewModels.HeroViewModel model, FormCollection f)
        {
            try
            {
                string HeroTypeId = ConvertStringArrayToString((string[])f.GetValue("HeroType").RawValue);
                if (HeroTypeId == string.Empty)
                {
                    ModelState.AddModelError(string.Empty, "Hero Type not selected. Please chose one");
                    return(View(model));
                }
                model.HeroId = ObjectId.Parse(ConvertStringArrayToString((string[])f.GetValue("HeroId").RawValue));

                LoadHeroTypes(HeroTypeId);

                model.HeroType = _heroTypeAppService.GetById(ObjectId.Parse(HeroTypeId));
                var validation = _heroAppService.Update(model);
                if (validation.IsValid)
                {
                    ViewBag.Success = validation.Message;
                }
                else
                {
                    foreach (var error in validation.Erros)
                    {
                        ModelState.AddModelError(string.Empty, error.Message);
                    }
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View(model));
            }
        }