2

同じコントローラー内の[HttpPost]アクションからアクションにリダイレクトしたい。[HttpGet]

出来ますか?

ログインは複雑であり、典型的なログオン機能ではないため、同じコードをログイン用に書き直さないようにしています。

これは私のコードです:

[HttpGet]
public ActionResult Login(){
    return View();
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    //search user and create sesion
    //...

    if (registered)
        {
        return this.RedirectToAction("Home","Index");                      
        }else{
        return this.RedirectToAction("Home", "signIn");
    }
}

[HttpGet]
public ActionResult signIn(){
    return View();
}

[HttpGet]
public ActionResult signInUserModel model)
{
    //Register new User
    //...
    if (allOk)
        {
        LoginModel loginModel = new LoginModel();
            loginModel.User = model.User;
            loginModel.password = model.password;

            //next line doesn't work!!!!!
        return this.RedirectToAction("Home","Login", new { model = loginModel); 

        }else{
        //error
        //...

    }

}

どんな助けでも大歓迎です。

ありがとう

4

3 に答える 3

3

メソッド名から別のビューを返すことができます

public ActionResult signInUserModel model)
{
    ...
    return View("Login", loginModel);
}

少し遅れましたが、同じ問題が発生しました...

于 2013-12-10T10:26:49.903 に答える
2

Post メソッドからコア ログイン ロジックをリファクタリングし、両方の場所からその新しいメソッドを呼び出すことができます。

たとえば、誰かのログインを処理するために LoginService クラスを作成するとします。これは両方のアクションからそれを使用する場合にすぎないため、一方のアクションから他方のアクションにリダイレクトする必要はありません。

于 2013-01-17T14:02:28.157 に答える
1

可能です。すべてのアクションは同じコントローラー内にある必要があります。

public ActionResult Login(){
    return View();
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    //search user and create sesion
    //...

    if (registered)
    {
        return RedirectToAction("Index");
    }

    return View(model);
}

public ActionResult SignIn(){
    return View();
}

[HttpPost]
public ActionResult SignIn(UserModel model)
{
    //Register new User
    //...
    if (allOk)
    {
        return RedirectToAction("Login");
    }

    return View(model);
}
于 2013-01-17T14:05:40.100 に答える