asp.net-mvc 我可以通过 Ajax 请求从 MVC 控制器返回 javascript 到视图吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19541336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can i return javascript from MVC controller to View via Ajax request
提问by Sai Avinash
I am trying to do like this in ASP.net MVC 2.0 Application. I have a form with two fields number1 and number2.
我试图在 ASP.net MVC 2.0 应用程序中这样做。我有一个包含两个字段 number1 和 number2 的表单。
I want add two these two numbers using an ajax request. In the controller, i am recieving two numbers ,adding them and storing result in another string. then, i am doing like this:
我想使用 ajax 请求添加两个这两个数字。在控制器中,我收到两个数字,将它们相加并将结果存储在另一个字符串中。然后,我这样做:
[HttpPost]
public string TestAjax(FormCollection form)
{
int strnum1 = Convert.ToInt32(form["txtbox1"].ToString());
int strnum2 = Convert.ToInt32(form["txtbox2"].ToString());
string strnum3 = Convert.ToString(strnum1 + strnum2);
if (strnum3 != null)
{
return "<script>alert("some message")</script>";
}
return string.Empty;
}
Is it possible to return java script in the form of string from controller actions
是否可以从控制器动作以字符串的形式返回java脚本
回答by Darin Dimitrov
Is it possible to return java script in the form of string from controller actions
是否可以从控制器动作以字符串的形式返回java脚本
You could return a JavaScriptResult:
你可以返回一个JavaScriptResult:
[HttpPost]
public ActionResult TestAjax(FormCollection form)
{
int strnum1 = Convert.ToInt32(form["txtbox1"].ToString());
int strnum2 = Convert.ToInt32(form["txtbox2"].ToString());
return JavaScript("<script>alert(\"some message\")</script>");
}
For this to work you have to configure your AJAX request to execute javascript. For example if you are using jQuery that would look like this:
为此,您必须配置 AJAX 请求以执行 javascript。例如,如果您使用的 jQuery 如下所示:
$.ajax({
url: '/someaction',
type: 'POST',
data: { txtbox1: '12', txtbox2: '13' },
dataType: 'script'
});
As an alternative you could return a JsonResultwhich will serialize the model to a JSON string:
作为替代方案,您可以返回一个JsonResult,它将模型序列化为 JSON 字符串:
[HttpPost]
public ActionResult TestAjax(FormCollection form)
{
int strnum1 = Convert.ToInt32(form["txtbox1"].ToString());
int strnum2 = Convert.ToInt32(form["txtbox2"].ToString());
string strnum3 = Convert.ToString(strnum1 + strnum2);
return Json(new { sum = strnum3 });
}
and then:
进而:
$.ajax({
url: '/someaction',
type: 'POST',
data: { txtbox1: '12', txtbox2: '13' },
success: function(result) {
alert(result.sum);
}
});
As you can see here you are no longer mixing javascript with C# code in your controller. Respect the DRY principle. javascript belongs in javascript files.
正如您在此处看到的,您不再将 javascript 与控制器中的 C# 代码混合在一起。尊重 DRY 原则。javascript 属于 javascript 文件。
As further improvement to this controller action I would recommend you introducing a view model and get rid of the terrible plumbing code of parsing stuff from the FormCollection. That's the responsibility of the default model binder.
作为对该控制器操作的进一步改进,我建议您引入一个视图模型并摆脱从 FormCollection 解析内容的可怕管道代码。这是默认模型绑定器的责任。
So:
所以:
public class SumViewModel
{
public int TxtBox1 { get; set; }
public int TxtBox2 { get; set; }
}
and then:
进而:
[HttpPost]
public ActionResult TestAjax(SumViewModel model)
{
int sum = model.TxtBox1 + model.TxtBox2;
return Json(new { sum = sum });
}
Conclusion: we have put this controller action on a diet, and moved the javascript where it belongs.
结论:我们已经放弃了这个控制器动作,并将 javascript 移到它所属的地方。
回答by Mike H.
Why not use your ajax success callback function?
为什么不使用你的 ajax 成功回调函数?
[HttpPost]
public string TestAjax(FormCollection form)
{
int strnum1 = Convert.ToInt32(form["txtbox1"].ToString());
int strnum2 = Convert.ToInt32(form["txtbox2"].ToString());
string strnum3 = Convert.ToString(strnum1 + strnum2);
if (strnum3 != null)
{
return strnum3;
}
return string.Empty;
}
$.ajax({
url: "/Controller/TestAjax/",
type: "POST",
data: /* form data here */,
success: SuccessCallback,
error: FailureCallback
});
function SuccessCallback(data) {
alert(data);
}
回答by Jeet Bhatt
Try this,
尝试这个,
[HttpPost]
public ActionResult TestAjax(FormCollection form)
{
int strnum1 = Convert.ToInt32(form["txtbox1"].ToString());
int strnum2 = Convert.ToInt32(form["txtbox2"].ToString());
string strnum3 = Convert.ToString(strnum1 + strnum2);
if (strnum3 != null)
{
return "<script>alert("some message")</script>";
}
return JavaScript("JSFunction(paramtr);");
}
View:-
看法:-
function JSFunction(paramtr)
{
}

