3

HTML入力テキストフィールドとボタンがあります。

そのボタンをクリックしてそのhtmlテキストフィールドからユーザー入力値を取得し、その値を(AJAXによって)MVC3コントローラーに送信したい(ActionResult setValue()のパラメーターのように)?

私が知りたいもう1つのことは、MVC3コントローラーから戻り値(ActionResult getValue()によって返される)を取得し、それをhtmlテキストフィールド(AJAXによって)に設定する方法を教えてください。

良い例を教えてください。英語が下手でごめんなさい。:)

4

1 に答える 1

10

ボタンクリックイベント

$(document).ready(function ()
{
    $('#ButtonName').click(function ()
    {
        if ($('#YourHtmlTextBox').val() != '')
        {
            sendValueToController();
        }
        return false;
    });
});

次のようにajax関数を呼び出します。

function sendValueToController()
{
    var yourValue = $('#YourHtmlTextBox').val();

    $.ajax({
        url: "/ControllerName/ActionName/",
        data: { YourValue: yourValue },
        cache: false,
        type: "GET",
        timeout: 10000,
        dataType: "json",
        success: function (result)
        {
            if (result.Success)
            { // this sets the value from the response
                $('#SomeOtherHtmlTextBox').val(result.Result);
            } else
            {
                $('#SomeOtherHtmlTextBox').val("Failed");
            }
        }
    });
}

これが呼び出されているアクションです

public JsonResult ActionName(string YourValue)
{
    ...
    return Json(new { Success = true, Result = "Some Value" }, JsonRequestBehavior.AllowGet);
}
于 2012-06-29T18:41:42.427 に答える