Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript对象传递给C#MVC4控制器_Javascript_C#_Ajax_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

如何将javascript对象传递给C#MVC4控制器

如何将javascript对象传递给C#MVC4控制器,javascript,c#,ajax,asp.net-mvc,asp.net-mvc-4,Javascript,C#,Ajax,Asp.net Mvc,Asp.net Mvc 4,在MVC4中,如何在AJAX中将javascript对象传递给C#控制器? 最后我试了一下,但没有成功 Javascript客户端: var myData = {Propr1: '', Propr2: ''}; $.ajax({ type: 'POST', data: JSON.stringify(myData), url: '/Home/SubmitMyData', contentType: 'applicatio

在MVC4中,如何在AJAX中将javascript对象传递给C#控制器? 最后我试了一下,但没有成功

Javascript客户端:

    var myData = {Propr1: '', Propr2: ''};
    $.ajax({
        type: 'POST',
        data: JSON.stringify(myData),
        url: '/Home/SubmitMyData',
        contentType: 'application/json',
        dataType: 'json',
        success: alert('Youhou'),
        error: alert('not good')
    });
    public ActionResult SubmitMyData(MyParamModel myParam)
    {
        // Do my stuff here with my parameter
        return View();
    }

    public class MyParamModel
    {
        string Prop1 { get; set; }
        string Prop2 { get; set; }
    }
    var myData = {Prop1: '', Prop2: ''}; // #1
    $.ajax({
        type: 'POST',
        data: myData, // #2
        url: '/Home/SubmitMyData',
        //contentType: 'application/json', #3
        //dataType: 'json', #2
        success: alert('Youhou'),
        error: alert('not good')
    });
    public ActionResult SubmitMyData(MyParamModel myParam)
    {
        // Do my stuff here with my parameter
        return View();
    }

    public class MyParamModel // #4
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
C#服务器端方法:

    var myData = {Propr1: '', Propr2: ''};
    $.ajax({
        type: 'POST',
        data: JSON.stringify(myData),
        url: '/Home/SubmitMyData',
        contentType: 'application/json',
        dataType: 'json',
        success: alert('Youhou'),
        error: alert('not good')
    });
    public ActionResult SubmitMyData(MyParamModel myParam)
    {
        // Do my stuff here with my parameter
        return View();
    }

    public class MyParamModel
    {
        string Prop1 { get; set; }
        string Prop2 { get; set; }
    }
    var myData = {Prop1: '', Prop2: ''}; // #1
    $.ajax({
        type: 'POST',
        data: myData, // #2
        url: '/Home/SubmitMyData',
        //contentType: 'application/json', #3
        //dataType: 'json', #2
        success: alert('Youhou'),
        error: alert('not good')
    });
    public ActionResult SubmitMyData(MyParamModel myParam)
    {
        // Do my stuff here with my parameter
        return View();
    }

    public class MyParamModel // #4
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
我的参数总是空的。我试图更改contentType,但仍然不起作用。 我的错误在哪里?我找到了一些帖子,但我没有发现我做错了什么

非常感谢

  • 确保javascript和C#模型之间的属性名称匹配。在您的问题中,javascript对象有
    Propr1
    Propr2
    ,但在C#模型中,您有
    Prop1
    Prop2
    (缺少“r”)
  • 在发送数据之前,不要对数据进行字符串化,也不要将数据类型设置为json。MVC可以很好地解析post参数集合,而无需在代码中进行json序列化
  • 省略
    内容类型
    ,它不是必需的。WebAPI应该自动检测到这一点。你可以离开,但这是无关的
  • 确保模型属性是公共的
  • Javascript客户端:

        var myData = {Propr1: '', Propr2: ''};
        $.ajax({
            type: 'POST',
            data: JSON.stringify(myData),
            url: '/Home/SubmitMyData',
            contentType: 'application/json',
            dataType: 'json',
            success: alert('Youhou'),
            error: alert('not good')
        });
    
        public ActionResult SubmitMyData(MyParamModel myParam)
        {
            // Do my stuff here with my parameter
            return View();
        }
    
        public class MyParamModel
        {
            string Prop1 { get; set; }
            string Prop2 { get; set; }
        }
    
        var myData = {Prop1: '', Prop2: ''}; // #1
        $.ajax({
            type: 'POST',
            data: myData, // #2
            url: '/Home/SubmitMyData',
            //contentType: 'application/json', #3
            //dataType: 'json', #2
            success: alert('Youhou'),
            error: alert('not good')
        });
    
        public ActionResult SubmitMyData(MyParamModel myParam)
        {
            // Do my stuff here with my parameter
            return View();
        }
    
        public class MyParamModel // #4
        {
            public string Prop1 { get; set; }
            public string Prop2 { get; set; }
        }
    
    C#服务器端方法:

        var myData = {Propr1: '', Propr2: ''};
        $.ajax({
            type: 'POST',
            data: JSON.stringify(myData),
            url: '/Home/SubmitMyData',
            contentType: 'application/json',
            dataType: 'json',
            success: alert('Youhou'),
            error: alert('not good')
        });
    
        public ActionResult SubmitMyData(MyParamModel myParam)
        {
            // Do my stuff here with my parameter
            return View();
        }
    
        public class MyParamModel
        {
            string Prop1 { get; set; }
            string Prop2 { get; set; }
        }
    
        var myData = {Prop1: '', Prop2: ''}; // #1
        $.ajax({
            type: 'POST',
            data: myData, // #2
            url: '/Home/SubmitMyData',
            //contentType: 'application/json', #3
            //dataType: 'json', #2
            success: alert('Youhou'),
            error: alert('not good')
        });
    
        public ActionResult SubmitMyData(MyParamModel myParam)
        {
            // Do my stuff here with my parameter
            return View();
        }
    
        public class MyParamModel // #4
        {
            public string Prop1 { get; set; }
            public string Prop2 { get; set; }
        }
    

    数据
    属性传递的值应该是对象,而不是字符串:

    data: myData,
    
    属性名称需要匹配:

    var myData = { Prop1: '', Prop2: ''};
    
    您需要对参数值使用
    [FromBody]
    属性:

    public ActionResult SubmitMyData([FromBody] MyParamModel myParam)
    
    模型类型上的属性必须是公共的:

    public class MyParamModel
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
    

    你能解释一下什么是错误的以及为什么你的代码能工作吗?我认为FromBody属性在这里不合适。OP使用的是MVC 4,而不是Web API。@DavidL感谢您指出这一点,我认为这是一个Web API问题,因为标题的缘故。@danludwig说得不错。也许OP应该调整一下标题:)。对不起,标题。我又试了一次,但它对我不起作用:s我的参数仍然为null[binding]了解基本情况会很有帮助