私は MVC4 プロジェクトに取り組んでおり、同じコントローラーに同じ名前とパラメーターを持つ 2 つのアクションがあります。
public ActionResult Create(CreateBananaViewModel model)
{
if (model == null)
model = new CreateBananaViewModel();
return View(model);
}
[HttpPost]
public ActionResult Create(CreateBananaViewModel model)
{
// Model Save Code...
return RedirectToAction("Index");
}
既存のモデルを Create メソッドに渡したい理由は、既存のモデルを複製してから変更するためです。
明らかにコンパイラはこれを好まないので、1 つのメソッドを次のように変更しました。
[HttpPost]
public ActionResult Create(CreateBananaViewModel model, int? uselessInt)
{
// Model Save Code...
return RedirectToAction("Index");
}
これは完全に受け入れられますか?または、この問題を回避するためのより良い方法はありますか?
編集/解決策:
状況を完全に複雑にしすぎたようです。これが私の解決策です
public ActionResult Duplicate(Guid id)
{
var banana = GetBananaViewModel(id);
return View("Create", model);
}
public ActionResult Create()
{
var model = new CreateBananaViewModel();
return View(model);
}