我目前有以下代码用于POST来编辑客户备注。
[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 :(得分:0)
使用帖子来点击此操作。难道你不希望params作为表单的一部分而不是url中出现吗?
如果您确实需要它,我想您可以对包含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的代码。缺点(虽然我看不到它在这里做了什么)是你没有返回验证失败。