Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //Объект
  2. public class Fcu : Entity<int>
  3. {
  4. public string Name { get; set; }
  5. public int FcuTypeId { get; set; }
  6. public virtual FcuType FcuType { get; set; }
  7. }
  8. //Навигационное свойтво
  9. public class FcuType : Entity<int>
  10. {
  11. //ctor
  12. public string Name { get; set; }
  13. public virtual ICollection<Fcu> Fcus { get; set; }
  14. }
  15.  
  16. [HttpPost]
  17. public ActionResult Add(FcuCreateViewModel model)
  18. {
  19. if (!ModelState.IsValid)
  20. return PartialView("~/Views/Fcu/_Add.cshtml", model);
  21.  
  22. var entity = new Fcu();
  23. entity.Name = model.Name;
  24. entity.FcuTypeId = model.SelectedFcuTypeId;
  25.  
  26. // 1. Сохраняем
  27. _fcuService.Save(entity);
  28. // 2. Сохранили, теперь нужно его вытащить, нужен объект с уже
  29. // заполненным Id, т.к. Id не известен передаем Name
  30. var result = _fcuService.GetByName(model.Name);
  31.  
  32. return Json(new { Fcu = result, Error = string.Empty });
  33. }
  34.  
  35. public Fcu GetByName(string name)
  36. {
  37. var result = _fcuRepository.Get(x => x.Name == name);
  38. return result;
  39. }
  40.  
  41. public T Get(Expression<Func<T, bool>> where)
  42. {
  43. return _dbSet.Where(where).FirstOrDefault<T>();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement