ASP.NET MVC和jQuery得到控制信息 [英] ASP.NET MVC and JQuery get info to controller

查看:85
本文介绍了ASP.NET MVC和jQuery得到控制信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就怎么做阿贾克斯的东西与jQuery完全糊涂了,似乎我越试越糊涂,我得到。在这一点上我想要做的就是使用jQuery AJAX的数据给我的控制器。有些$ C $下我的jQuery Ajax调用。

I am totally confused on how to do ajax stuffs with jQuery and it seems the more I try the more confused I get. At this point all I want to do is get data to my controller using jQuery ajax. Some code for my jquery ajax call is.

$(function() {
    $('#buttonAddLink').click(function() {
        var AjaxLink = {
            title: $("#linkTitle").val(),
            url: $("#linkUrl").val()
        };

        $.ajax({
            url: '/User/AddLink',
            type: 'POST',
            data: AjaxLink,
            dataType: 'json',
            success: function(result){
                $('.added').html(result.Result).show();
            }
         });         
    });    
});

下面是我的控制器和行动我使用。从想看看几个教程就应该工作,尽我所知,但显然我不明白像我想我做到了。

Here is my controller and Action I am using. From trying to look at several tutorials it "should" work to the best of my knowledge, but apparently I don't get it like I thought I did.

public ActionResult AddLink(string title, string url)
{
    return Json(new { Result = string.Format(title + " " + url)});
}

所有我基本上试图做的是做的Ajax调用和返回它来显示只是为了确保数据被发送到控制器。

All I am basically trying to do with that is do the Ajax call and return it to display just to make sure data was sent to the controller.

推荐答案

它,是因为有你在构建你的请求的方式。您JQuery的通话将数据发送到用户控制器POST数据,这意味着在C#code在AddLink动作,你会通过的Request.Form对象来访问它。随着你想做什么,你就构建了jQuery网址

It has to do with the way you're structuring your request. Your JQuery call is sending the data to the AddLink action on the User controller as POST data, which means in your C# code, you'll access it via the Request.Form object. With what you're trying to do, you'd structure the jQuery URL as

/User/AddLink/{Title}/{URL}

这将要求你写在Global.asax文件的规则处理之类的输入。总之,如果你只是修改AddLink方法如下:

This would require you to write a rule in your Global.ASAX file that handled that sort of input. In short, if you just modify your AddLink method as follows:

public ActionResult AddLink()
{
    return Json(new { Result = string.Format(Request.Form["title"] + " " + Request.Form["url"])});
}

我相信你会得到你要找的响应。

I believe you'll get the response you're looking for.

这篇关于ASP.NET MVC和jQuery得到控制信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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