2

これは簡単です。(bool に応じて) 新しいビューを返すコントローラーを呼び出すビューがあります。そうでない場合は、現在のビューにとどまります。

どうすればいいですか?

私の現在のコントローラーコードは次のようなものです:

public class MyController : Controller
{
    public ActionResult someView(bool myBool) // is really a string
    {
        if (myBool) // Is really checking if the string is empty
        {
            ModelState.AddModelError("message", "This is true");
        }
        else
        {
            return View();
        }
        return null;
    }
}

mvc4 についてもっと学ぶ必要があることはわかっていますが、一緒に遊んでください ;-D


私のキャプテンスカイホークのために編集 ^^

_Partial ページ コード: ( Thanks to John H )

@using (Html.BeginForm("someView", "My", FormMethod.Get))
{
    @Html.TextBox("text")
    <input type="submit" value='send' />
}

Viewしかし、私の質問の本当の目標は、それを呼び出した元に戻る方法を見つけることです。うまくいけば、Modelあなたがいなくても、それは正しい方法です^^

4

3 に答える 3

3

別のビューにリダイレクトできます。

public class MyController : Controller
{
    public ActionResult someView(bool myBool)
    {
        if (myBool)
        {
            return View();
        }

        return RedirectToAction("actionname");
    }
}

RedirectToAction の他のパラメーターを使用して、コントローラー名と他のアクションへのパス アロングを指定することもできます。

于 2013-11-07T21:04:15.377 に答える
3

return null意味がありません。それは本質的に「ビューを返さない」と言っています。このようなものが動作します:

public class MyController : Controller
{
    public ActionResult SomeView(bool myBool)
    {
        if (myBool)
        {
            ModelState.AddModelError("message", "This is true");
            return View();
        }

        return RedirectToAction("SomeOtherView");
    }
}

これは、私があなたの他の質問で言及したパターンに近いModelState.IsValidものです (これは次のようになります:

public ActionResult SomeView(SomeViewModel model)
{
    if (ModelState.IsValid)
    {
       // Model is valid so redirect to another action
       // to indicate success.
       return RedirectToAction("Success");
    }

    // This redisplays the form with any errors in the ModelState
    // collection that have been added by the model binder.
    return View(model);
}
于 2013-11-07T21:05:37.990 に答える
1

このようなことをしてください

public class MyController : Controller
{
public ActionResult someView(bool myBool)
{
    if (myBool)
    {
        return View("someView");
      // or return ReddirectToAction("someAction")
    }
    else
    {
        return View();
    }

 }

}

于 2013-11-07T21:07:13.160 に答える