2011-01-31 8 views
1

ユーザーがレコードを削除した後、戻る矢印を押して、POST要求を再送信するとします。削除アクションのユーザー再送信を処理する適切な方法はありますか?

このシナリオを処理する際の選択肢は何ですか?

推奨されるのは?

[HttpPost] 
    public ActionResult Delete(string EntryName, Guid id, FormCollection collection) 
    { 
     try 
     { 
      var ret = from m in _entities.MyList 
         where m.MyListID == id 
         && m.EntryName == EntryName 
         select m ; 

      if (ret.Count() == 0) 
      { 
       // This happens if the user pressed the back button and resubmitted 
       // todo: ask SO what is the best way to approach this... 
       // User feedback? How? 
       return RedirectToAction("Index", new { id = id }); 
      } 

      _entities.DeleteObject(ret.FirstOrDefault()); 
      _entities.SaveChanges(); 

      return RedirectToAction("Index", new { id = id }); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 
+0

:-)あそこニースTODOコメントは彼に彼が存在しません削除しようとするもの/既に削除されたという通知を表示します。 :) –

+0

どのようにそのUIはMVCで行われるのだろうか? – LamonteCristo

答えて

2

、より多くの詳細については参照してください。これを処理するRESTfulな方法が見つかりません404を投げることですモデル状態にエラーとは、ビュー再表示:

if (ret.Count() == 0) 
{ 
    ModelState.AddModelError("id", "An item with the specified id was not found"); 
    return View(); 
} 

を、ビュー内で次のメッセージを表示するために、検証の概要やidの検証メッセージを持っているでしょう。

P.S:

0

この場合、TempDataを使用したいと思うでしょう。

このビューでは、TempDataにアクセスしてメッセージを表示できます。もう一つの方法は、追加することです

if (ret.Count() == 0) 
{ 
    throw new HttpException(404, "Not found"); 
} 

:(ユーザーがもはや存在しないレコードを削除しようとしたため)このlink

関連する問題