3

私は 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);
}
4

1 に答える 1

5

modelGETCreateアクションでパラメータが本当に必要ですか? 次のようなことができます。

public ActionResult Create()
{
    var model = new CreateBananaViewModel();

    return View(model);
}

または、アクションにクエリ データを受け取りたい場合 ( www.mysite.com/banana/create?bananaType=yellow)

public ActionResult Create(string bananaType, string anotherQueryParam)
{
    var model = new CreateBananaViewModel()
    {
       Type = bananaType
    };
    return View(model);
}

POSTアクションをそのままにしておきます

[HttpPost]
public ActionResult Create(CreateBananaViewModel model) {}
于 2013-06-04T11:39:26.600 に答える