2010-01-28 67 views
0

我目前拥有以下代码来编辑客户注释。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和客户ID)这样的事情:“http://localhost:63137/Customers/EditNote/?NoteID=7&CustomerID=28

任何关于如何做到这一点的想法?

谢谢!

回答

0

此操作是通过使用帖子命中。你不希望这些参数作为表单的一部分而不是在网址中吗?

如果您确实需要它,我想您可以对包含noteId和customerId的编辑GET操作执行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 2010-01-28 17:31:18

+0

优秀。乐意效劳。 – 2010-01-28 17:34:36