public ActionResult Create(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));
                }
                LoadHeroTypes(HeroTypeId);

                model.HeroType = _heroTypeAppService.GetById(ObjectId.Parse(HeroTypeId));
                var validation = _heroAppService.Add(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));
            }
        }
示例#2
0
 public JsonResult Create(HeroVM hero)
 {
     try
     {
         hero = _heroAppService.Add(hero);
         return(Json(new { success = true, hero = hero }));
     }
     catch (Exception ex)
     {
         return(Json(new { success = false, message = "Error" }));
     }
 }
示例#3
0
        public HttpResponseMessage Post([FromBody] HeroViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError("Invalid Hero")));
            }

            try
            {
                var validation = _heroAppService.Add(model);
                if (validation.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, validation.ToJson()));
                }

                return(Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError(validation.Erros.ToJson())));
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }