2010-01-28 2 views
0

현재 고객 참고 사항을 편집하기 위해 POST에 대해 다음 코드가 있습니다. 검증이 실패하면ASP.NET MVC :보기를 반환 할 때 서버 유효성 검사 및 URL 매개 변수 유지

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult EditNote(Note note) 
    { 
     if (ValidateNote(note)) 
     { 
      _customerRepository.Save(note); 
      return RedirectToAction("Notes", "Customers", new { id = note.CustomerID.ToString() }); 
     } 
     else 
     { 
      var _customer = _customerRepository.GetCustomer(new Customer() { CustomerID = Convert.ToInt32(note.CustomerID) }); 
      var _notePriorities = _customerRepository.GetNotePriorities(new Paging(), new NotePriority() { NotePriorityActive = true }); 

      IEnumerable<SelectListItem> _selectNotePriorities = from c in _notePriorities 
                   select new SelectListItem 
                   { 
                    Text = c.NotePriorityName, 
                    Value = c.NotePriorityID.ToString() 
                   }; 

      var viewState = new GenericViewState 
      { 
       Customer = _customer, 
       SelectNotePriorities = _selectNotePriorities 
      }; 

      return View(viewState); 
     } 


    } 

, 나는 그것이 다시위한 editNote보기를 렌더링하지만,이 같은 뭔가를 URL 매개 변수 (NoteID와의 CustomerID)을 유지하려면 :이 작업을 수행하는 방법에 대한 "http://localhost:63137/Customers/EditNote/?NoteID=7&CustomerID=28"

어떤 아이디어?

감사합니다.

답변

0

이 작업은 게시물을 사용하여 해결됩니다. params가 url이 아닌 form의 일부로 나오기를 원하지 않습니까?

원한다면 noteId 및 customerId가 포함 된 편집 작업에 RedirectToAction을 수행 할 수 있다고 가정합니다.

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult EditNote(Note note) 
{ 
    if (ValidateNote(note)) 
    { 
     _customerRepository.Save(note); 
     return RedirectToAction("Notes", "Customers", new { id = note.CustomerID.ToString() }); 
    } 

    //It's failed, so do a redirect to action. The EditNote action here would point to the original edit note url. 
    return RedirectToAction("EditNote", "Customers", new { id = note.CustomerID.ToString() }); 
} 

이것의 장점은 당신이 고객, 노트와 wotnot를 얻을 코드를 복제 할 필요가 제거된다는 것입니다 : 이것은 효과적으로 같은 액션 모양을 만들 것입니다. 단점 (비록 내가 여기에서 그것을 어디서 볼 수는 없지만)은 검증 실패를 반환하지 않는다는 것이다.

+0

네가 맞아. 내 뇌의 방귀. 나에게 필요한 불꽃을 주셔서 감사합니다. 이제 양식을 통해오고있어 작동 중입니다. 감사! – Mike

+0

우수. 기꺼이 도와주세요. –