public ProductEntity ProductMapper(CreatedProductViewModel source) { if (source == null) { return null; } var target = Mapper.Map<CreatedProductViewModel, ProductEntity>(source); return target; }
public ActionResult Create(FormCollection formCollection, CreatedProductViewModel model) { if (ModelState.IsValid) { var entity = _mapperManager.ProductMapper(model); entity.CreatedDate = DateTime.Now; entity.CreatedUser = base.CurrentUser.CustomerId; entity.UpdatedDate = DateTime.Now; entity.UpdatedUser = base.CurrentUser.CustomerId; entity.Brand = _brandRepository.Find(entity.Brand_Id); entity.Store = _storeRepository.Find(entity.Store_Id); entity.Tag = _tagRepository.Find(entity.Tag_Id); entity.Favorable = string.Empty; entity.RecommendedReason = string.Empty; entity.RecommendUser = base.CurrentUser.CustomerId; entity.Status = (int)DataStatus.Normal; entity = _productRepository.Insert(entity); //var r = Get(e.Id); return new RestfulResult { Data = new ExecuteResult<int>(entity.Id) { StatusCode = StatusCode.Success, Message = "" } }; } else { // 如果我们进行到这一步时某个地方出错,则重新显示表单 //var dto = new CreateDto { Tags = tagList }; //dto.Vo = vo; //dto.IsError = true; //return View("Success",new SuccessViewModel{}); List<string> sb = new List<string>(); //获取所有错误的Key List<string> Keys = ModelState.Keys.ToList(); //获取每一个key对应的ModelStateDictionary foreach (var key in Keys) { var errors = ModelState[key].Errors.ToList(); //将错误描述添加到sb中 foreach (var error in errors) { sb.Add(error.ErrorMessage); } } return new RestfulResult { Data = new ExecuteResult<List<string>>(sb) { StatusCode = StatusCode.ClientError, Message = "验证失败" } }; } }
public ActionResult Create() { var model = new CreatedProductViewModel(); model.Brands = _brandRepository.Get( v => v.Status == (int)DataStatus.Normal && v.CreatedUser == CurrentUser.CustomerId).ToList(); model.Stores = _storeRepository.Get(v => v.Status == (int)DataStatus.Normal && v.CreatedUser == CurrentUser.CustomerId).ToList(); model.Tags = _tagRepository.Get(v => v.Status == (int)DataStatus.Normal && v.CreatedUser == CurrentUser.CustomerId).ToList(); return View(model); }