1

WPF のバックグラウンドを離れた後、Asp.Net MVC を学習中です。私は基本的にボタンをクリックして、ページを更新せずにモデルを更新したいと考えています。どういうわけかAJAXで次のことができると思います。

<input class="WelcomeButton" type="button" onclick="location.href='@Url.Action("SetGameType", "Welcome", new { gameType = GameTypes.Expert })'"/>

どんな助けでも素晴らしいでしょう。

これは、Gent's Link を見た後の私の進歩です。

<input class="WelcomeButton" id="NoviceButton" type="button" style="left: 157px; top: 442px;"/>

$("#NoviceButton").click(function(){
    $.ajax({
        url: "@Url.Action("TestMethod", "Welcome")",
        type: "post"
    });
});

送信するボタンがあり、フォームにボタンがありません...いずれにしても、上記のURLまたはこのURLでは機能しませんでしたurl: "\Welcome\TestMethod",

4

3 に答える 3

2

はい、AJAX リクエストを作成する場合は、おそらく JavaScript フレームワークを使用する必要があります。ASP.NET MVC には JQuery が付属しているため、この例では、JQuery の準備がすべて整っていることを前提としています。

<button id="MyButton" />

<script type="text/javascript">
    // This will bind a javascript event handler to the button(s) identified
    // by the CSS selector #MyButton.
    //
    // You could also use the onclick event in the <input> element to 
    // say, call a javascript function... but this couples the HTML to
    // the Javascript logic in a way that is less maintainable as things
    // get more complex.
    $("#MyButton").click(function () {

        // $ is a global reference to JQuery. This instantiates and executes
        // a JQuery request using the browsers native request object (different
        // browsers do this slightly differently, this is why you need a 
        // framework like JQuery to write Javascript productively)
        $.ajax(

            // This is the URL back to an ASP.NET controller\action that will handle the event.
            url: "{controller}/{action}",

                            // This is a callback function that will execute when the round trip completes.
            success: function (response) {
                alert("server response: " + response);
            }
        );
    });
</script>

Web プログラミングの世界へようこそ。http://api.jquery.com/jQuery.ajax/

于 2012-08-12T02:48:06.677 に答える
1

knockoutJSを見てください。MVVM の設計パターンは、WPF の経験からおなじみのはずです。

于 2012-08-12T10:21:22.007 に答える
0

あなたがしようとしているのは、現在のページの状態を失うことなくサーバーを呼び出すことです。その場合は、onclick で URL をヒットさせる代わりに、ajax 投稿を行う JavaScript メソッドを呼び出します。この質問を読むことをお勧めします: jQuery Ajax POST example with PHP

于 2012-08-12T02:04:43.577 に答える