我可以通过Ajax请求将MV从MVC控制器返回到View [英] Can i return javascript from MVC controller to View via Ajax request

查看:227
本文介绍了我可以通过Ajax请求将MV从MVC控制器返回到View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ASP.net MVC 2.0应用程序中这样做。我有一个包含两个字段number1和number2的表单。

I am trying to do like this in ASP.net MVC 2.0 Application. I have a form with two fields number1 and number2.

我想使用ajax请求添加两个这两个数字。在控制器中,我收到两个数字,添加它们并将结果存储在另一个字符串中。然后,我这样做:

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:

    [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;

    }

是否可以以字符串的形式返回java脚本来自控制器操作

Is it possible to return java script in the form of string from controller actions

推荐答案


是否可以从控制器操作返回字符串形式的java脚本

Is it possible to return java script in the form of string from controller actions

您可以返回 JavaScriptResult

You could return a 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>");
}

为此,您需要配置AJAX请求以执行javascript。例如,如果您使用的jQuery如下所示:

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({
    url: '/someaction',
    type: 'POST',
    data: { txtbox1: '12', txtbox2: '13' },
    dataType: 'script'
});

作为替代方案,您可以返回 JsonResult ,它将模型序列化为JSON字符串:

As an alternative you could return a JsonResult which will serialize the model to a JSON string:

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

然后:

$.ajax({
    url: '/someaction',
    type: 'POST',
    data: { txtbox1: '12', txtbox2: '13' },
    success: function(result) {
        alert(result.sum);
    }
});

正如您在此处所见,您不再将javascript与控制器中的C#代码混合使用。尊重DRY原则。 javascript属于javascript文件。

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.

作为对此控制器操作的进一步改进,我建议您引入视图模型并摆脱从FormCollection解析内容的可怕管道代码。这是默认模型绑定器的责任。

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.

所以:

public class SumViewModel
{
    public int TxtBox1 { get; set; }
    public int TxtBox2 { get; set; }
}

然后:

[HttpPost]
public ActionResult TestAjax(SumViewModel model)
{
    int sum = model.TxtBox1 + model.TxtBox2;
    return Json(new { sum = sum });
}

结论:我们把这个控制器动作放在节食上,并移动了javascript它属于哪里。

Conclusion: we have put this controller action on a diet, and moved the javascript where it belongs.

这篇关于我可以通过Ajax请求将MV从MVC控制器返回到View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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