GETとPOSTのアクションで、同じ引数を使う方法

最近覚えましたよorz

GETとPOSTで同じ引数になっちゃうじゃん・・・ってことありませんかね。

たとえば内容の表示だけだった・・・でも後からボタン追加せよって指令受けてしまったときです(;´Д`)

大抵は、GETとPOSTのアクションメソッドの引数は違うので問題ないんですがぁ。

やりたいことはこうなんですが・・・これではコンパイルエラーになっちゃいますね。(つーかIntelliSenseがうるさくてコンパイル前にわかっちゃいますがorz)

public ActionResult EditCountry(int? id)
{
    Country model = _db.Country.Find(id);
    return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditCountry(int? id)
{
    var model = _db.Country.Find(id);
    TryUpdateModel(model);
    _db.SaveChanges();
    return View();
}

つまり"public ActionResult EditCountry(int? id)"が2つになっちゃうです。

そこで、POSTのほうに(GETでもいいがね)ActionName属性を追加して、メソッドの名前変えちゃえば、同じ引数が使えちゃうんですよぉ。

で、POSTのほうはこうすればOK。

[HttpPost]
[ActionName("EditCountry")]
[ValidateAntiForgeryToken]
public ActionResult EditCountryPost(int? id)
{
    var model = _db.Country.Find(id);
    TryUpdateModel(model);
    _db.SaveChanges();
    return View();
}

これで、解決!