Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ASP.Net MVC return View with new Instance of Model instead of using Redirect To Action
  2. New
  3.  
  4.  public ActionResult New(string id)
  5.         {
  6.             var sysId= new Guid(id);
  7. .......
  8. ........
  9.  
  10.   string Details = pDto.Name + "(" + pDto.Code + ")";
  11.  
  12.             var vm= new ViewModel(id);
  13.             vm.Details = Details;
  14.  
  15.             return View(vm);
  16.         }
  17.  
  18.  
  19.  
  20.  public ActionResult Add(ViewModel vm)
  21.         {
  22.                         ViewModel vm= vm;
  23.  
  24.             if (ModelState.IsValid)
  25.             {
  26.  
  27.                               var dto= _qRepository.GetFeaturesBy(viewModel.Code);
  28.  
  29.                 if (dto!= null)
  30.                 {
  31.  
  32.                     ModelState.AddModelError("Code", "Code is already in Use.");
  33.  
  34.                     return View("New", viewModel);
  35.                 }
  36.  
  37.                 _productService.AddFeature(..........);
  38.                // ModelState.Clear();  -- this works          
  39.                 vm= new ViewModel(vm.pId) { Message = "Code" + viewModel.Code + " was added ......", Details = vm.Details };
  40.  
  41.             }
  42.  
  43.  
  44.           return  View ("New", vm);
  45.  
  46.         }