0

コントローラーから別のビューを返そうとしています。ただし、正しいビューが表示されますが、URL は同じままです。

これが私の見えている形/Company/Createです。

@using (Html.BeginForm("Create", "Company", FormMethod.Post)) 
{ 
 // Form here
}

基本的に、フォームとモデルは/Company/Createアクションに送信されます。提出されたモデルが有効な場合は、データを処理して /Company/Index ビューにリダイレクトします。

return View("Index");

おっしゃる通り正しいビューは表示されますが、URL(アドレスバー)はそのままですhttp://.../Company/Create

試しRedirectToAction("Index");てみました それも動作しません。そして、それは良いMVCプラクティスだとは思いません。私は単一のレイアウトを持っており、会社のビューは次のようにレンダリングされますRenderBody()

何か案は ?

ありがとう。

編集 :

これが私のアクションメソッドです。

[HttpPost]
public ActionResult Create(CompanyCreate model)
{
    /* Fill model with countries again */
    model.FillCountries();

    if (ModelState.IsValid)
    {
        /* Save it to database */
        unitOfWork.CompanyRepository.InsertCompany(model.Company);
        unitOfWork.Save();
        RedirectToAction("Index");
        return View();
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}
4

1 に答える 1

3

URL を変更したい場合は、別のアクションにリダイレクトする必要があります。

ただし、 RedirectToActionは即座にリダイレクトするのではなく、RedirectToRouteResultオブジェクトであるオブジェクトを返しActionResultます。

RedirectToActionしたがって、アクションの結果を返すだけです。

[HttpPost]
public ActionResult Create(CompanyCreate model)
{
    /* Fill model with countries again */
    model.FillCountries();

    if (ModelState.IsValid)
    {
        /* Save it to database */
        unitOfWork.CompanyRepository.InsertCompany(model.Company);
        unitOfWork.Save();
        return RedirectToAction("Index");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}
于 2012-06-30T18:02:30.507 に答える