2011-03-20 58 views
4

我正在将现有的ASP.NET应用程序转换为MVC2,并且我有一个现有的方法,通过jQuery使用Ajax调用,之前可以工作,但现在不起作用。所以看起来我需要做一些改变,因为使用了MVC2,我无法弄清楚。从ASP.NET MVC2应用程序执行Ajax调用的问题应用程序

我已经降低了代码的复杂度,但它仍然不起作用。这是我当前的代码:

该按钮触发jQuery脚本单击

function leaveComment() { 
if (validate()) { 
    $.ajax({ 
     type: "POST", 
     url: "/Pages/PostBlogComment", 
     data: "{'name':'Test','url':'Test','email':'Test','body':'Test','postid':'Test'}", 
     dataType: "json", 
     success: function (msg) { 
      //success code goes here 
     }, 
     error: function (msg) { 
      //error code goes here 
     } 
    }); 
} 

};

在我的控制器进行调用的页面,我创建了以下方法:

public string PostBlogComment(string name, string url, string email, string body, string postid) 
{ 
    return "This is a test"; 
} 

调试时,我可以看到PostBlogComment方法被调用,但是有我在这里面临的两个主要问题:

  1. 该方法的所有参数都接收为空,所以我没有有用的数据可以使用。现在进行测试,所有参数都以代码的形式发送,如Test
  2. 当返回结果给Ajax方法,错误路径被调用,而不是成功之路,甚至它的方法并返回的字符串为正常(即使在发送的参数是空白)

该错误可能是容易被发现,对于那些谁这些事情经常工作(或至少我希望如此:))

回答

10

这里是改变你需要做这项工作:

$.ajax({ 
    type: 'POST', 
    url: '/Pages/PostBlogComment', 
    data: { 
     name: 'Test', 
     url: 'Test', 
     email: 'Test', 
     body: 'Test', 
     postid: 'Test' 
    }, 
    success: function (result) { 
     alert(result.Value); 
    }, 
    error: function (msg) { 
     //error code goes here 
    } 
}); 

和控制器行动

public ActionResult PostBlogComment( 
    string name, 
    string url, 
    string email, 
    string body, 
    string postid 
) 
{ 
    return Json(new { Value = "This is a test" }); 
} 

这可以通过引入视图模型进行改进:

public class PostViewModel 
{ 
    public string Name { get; set; } 
    public string Url { get; set; } 
    public string Email { get; set; } 
    public string Body { get; set; } 
    public string Postid { get; set; } 
} 

然后:

public ActionResult PostBlogComment(PostViewModel model) 
{ 
    return Json(new { Value = "This is a test" }); 
} 

注意事项:

  1. data hash属性一个jQuery AJAX调用需要作为我的考试或者你会发送一个JSON编码的字符串,并且ASP.NET MVC的默认模型绑定器不知道如何解析为动作参数。在ASP.NET MVC 3中,由于JsonValueProviderFactory允许你发送JSON请求,所以这已经改变了。所以,如果你正在使用ASP.NET MVC 3,你可以发送这样和动作参数的AJAX请求会被正确绑定:

    $.ajax({ 
        type: 'POST', 
        url: '/Pages/PostBlogComment', 
        data: JSON.stringify({ 
         name: 'Test', 
         url: 'Test', 
         email: 'Test', 
         body: 'Test', 
         postid: 'Test' 
        }), 
        contentType: 'application/json', 
        success: function (result) { 
         alert(result.Value); 
        }, 
        error: function (msg) { 
         //error code goes here 
        } 
    }); 
    
  2. 在ASP.NET MVC所有的控制器动作必须返回ActionResults。所以如果你想要Json,请返回JsonResult

  3. 动作传递一个匿名类型包含其在success回调和来自服务器的响应中使用的Value属性Json方法是这样的:

    { 'Value': 'This is a test' } 
    
  4. 决不硬编码这样的网址在您的JavaScript文件或您的应用程序可能会在您部署它时破坏。

    ... 
    url: '<%= Url.Action("PostBlogComment", "Pages") %>', 
    ... 
    

    ,或者这是一个外部JavaScript文件,你既可以使用在你看来初始化一些全局JS变量指向正确的URL或者使这个网址的一部分:使用URL打交道时总是使用网址帮手您的DOM(例如作为锚href属性或HTML5 data-*属性),然后使用jQuery来获取值。

+0

工作就像一个魅力!非常感谢Darin。 – 2011-03-20 22:26:26

+0

编辑后我只好说哇!一个非常全面和翔实的答案。希望我能给你一些额外的赞扬;) – 2011-03-20 23:10:47