通过AJAX发送到MVC3控制器并从中获取价值 [英] Send to and get value from a MVC3 controller by AJAX

查看:45
本文介绍了通过AJAX发送到MVC3控制器并从中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html输入文本字段和一个按钮.

I have a html input text field and a button.

我想通过单击该按钮从该html文本字段中获取用户输入值,并希望将该值(通过AJAX)发送到MVC3控制器中(就像ActionResult setValue()的参数一样)?

I want to take user input value from that html text field by clicking that button and want to send that value (by AJAX) into a MVC3 Controller ( as like as a parameter of an ActionResult setValue() ) ?

我想知道的另一件事是,如何从MVC3控制器获取返回值(由ActionResult getValue()返回)并将其设置在html文本字段中(由AJAX实现)?

An other thing i want to know that, how i can get a return value (that return by a ActionResult getValue() ) from a MVC3 Controller and set it in a html text field (by AJAX) ?

请帮我举个好榜样.对不起,我的英语不好. :)

please help me with a good example, please. and sorry for my bad English. :)

推荐答案

按钮单击事件

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

您可以像这样调用ajax函数:

You call your ajax function like so:

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);
}

这篇关于通过AJAX发送到MVC3控制器并从中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆