0

ASP.NET MVC3 プロジェクトには、2 つのコントローラーがあります。

HomeController.cs

public class HomeController : Controller
{
    //
    // GET: /Home/

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

    //
    // POST: /Home/CheckLogin/

    [HttpPost]
    public ActionResult CheckLogin()
    {
        // setting the session variable if login is correct
        // and redirecting to /ReWeb/

        // else, reloading the login.
    }
}

もう1つはReWebController.csです

public class ReWebController : Controller
{
    //
    // GET: /ReWeb/

    public ActionResult Index()
    {
        // Session verification
        if (Session["_ReWeb"] != null)
        {
            return View();
        }

        // If session is null or not valid,
        // redirect to login page
        return RedirectToAction("Index", "Home");
    }
}

HomeController Indexアクションによって返されるビューであるログイン ページには、次のフォームがあります。

<div id="loginPanel">
    <form enctype="application/x-www-form-urlencoded" id="login" method="post">
        <h1>Log In</h1>
        <fieldset id="inputs">
            <input id="Utente" name="Utente" type="text" placeholder="Utente" autofocus="true" required="true" />   
            <input id="Password" name="Password" type="password" placeholder="Password" required="true" />
        </fieldset>
        <fieldset id="actions">
            <input type="submit" id="submit" value="Log in" onclick="javascript:window.open('/Home/CheckLogin/', 'WebClient', 'width=500,height=250,left=100,top=100')"/>
        </fieldset>
    </form>
</div>

私がやろうとしているのは、ユーザーが[ログイン] ボタンをクリックすると、アプリケーションがポップアップ ページでReWebControllerのビューを返すことです。ここで行ったことから、ポップアップが開きますが、404 エラーが表示されます: '/' アプリケーションでサーバー エラーが発生しました。リソースが見つかりません。要求された URL: /Home/CheckLogin/

どうすればこのアプローチを実現できますか? 私はそれを正しくやっていますか?

どうもありがとう!

4

2 に答える 2

1

HomeController には POST 要求を受け入れる CheckLogin アクションしか含まれていないため、404 エラーが発生しています。ログインボタンをクリックすると、新しいウィンドウに URL が読み込まれます。これは GET リクエストです。

これを処理して、ログインが成功したかどうかに応じて、CheckLogin アクションが true または false を含む JSON オブジェクトを返すようにする 1 つのオプション。ログイン ビューでは、次のようなことができます。

$('form#login').submit(function () {
    var data = {}; // put the login data in here

    $.post('/Home/CheckLogin', data, function (result) {
        // Here you can check 'result'
        if (result.success === true) {
            window.open('/ReWeb', 'WebClient', 'width=500,height=250,left=100,top=100');
        }
        else {
            // The login failed
        }
    }).error(function() {
        // Something went wrong when trying to make the request.
        // Like a 404, 501, ... error
    });

    return false;
});
于 2012-09-18T10:02:17.753 に答える
0

フォーム要素にaction="Checklogin"を追加するだけです。

于 2012-09-18T13:43:18.620 に答える