gpt4 book ai didi

asp.net-mvc-3 - 通过 AJAX 发送到 MVC3 Controller 并从中获取值

转载 作者:行者123 更新时间:2023-12-01 00:43:02 25 4
gpt4 key购买 nike

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

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

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

请帮我举一个好的例子。抱歉我的英语不好。 :)

最佳答案

按钮点击事件

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

关于asp.net-mvc-3 - 通过 AJAX 发送到 MVC3 Controller 并从中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11267105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com