简体   繁体   English

从Ajax向MVC控制器传递值时为空值

[英]Null value when passing value from ajax to MVC controller

I have a value that I want to pass to MVC action, 我有一个要传递给MVC操作的值,

here is the JS side 这是JS面

function functionName(Name) {
        $.ajax({
            url: "/Home/GetName",
            type: "POST",
            dataType: "json",  
            data:JSON.stringify({ 
                Name: Name
            }),
            success: function (mydata) {

            }
        });
        return false;
    }

and here is my action 这是我的行动

[HttpPost]
public JsonResult GetName(string Name)
{
       return Json(new { oid = Name});
}

Noting that I successfully print the value "Name" before I send it to the action, but the action is receiving it as a "null" 请注意,在将值“名称”发送给操作之前,我已经成功打印了该值,但操作将其接收为“空”

Stephen "in the comments" is correct the solution is changing this 斯蒂芬“在评论中”是正确的解决方案正在改变这一点

data:JSON.stringify({ 
                Name: Name
            }),

to

data: { Name: Name } 

Try by adding 尝试添加

contentType: "application/json"

as belove 作为爱人

 function functionName(Name) {
    $.ajax({
        url: "/Home/GetName",
        type: "POST",
        dataType: "json",  
        contentType: "application/json",
        data:JSON.stringify({ 
            Name: Name
        }),
        success: function (mydata) {

        }
    });
    return false;
}

Hope this will help you, :) 希望这个能对您有所帮助, :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM