weixin_33725270 2017-05-18 16:42 采纳率: 0%
浏览 53

MVC模型绑定jQuery Ajax

I have a Problem with my Modelbinding and i cant figure it out why it wouldnt work. Heres the Controller:

    public class ItemListController : Microsoft.AspNetCore.Mvc.Controller
{

    [HttpPost]
    public async Task<IActionResult> Create(string name)
    {           
        return View();
    }

}

this is the javascript

  var myObject = JSON.stringify("{Name: 'Test'}");

    var dataobject = 
    $.ajax({
        type: "POST",
        url: "/ItemList/Create",
        data: myObject,
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        alert(data);
    }

    function errorFunc() {
        alert('error');
        console.log(JSON.stringify({ a: myData }));
    }
});

If tried nearly everything ive read. Can someone please let me know why binding is not happening?

  • 写回答

1条回答 默认 最新

  • ??yy 2017-05-18 16:44
    关注

    you can do it without JSON.stringify :

    $.ajax({
        type: "POST",
        url: "/ItemList/Create",
        data: {Name: 'Test'},
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: successFunc,
        error: errorFunc
    });
    
    function successFunc(data, status) {
        alert(data);
    }
    
    function errorFunc() {
        alert('error');
        console.log(JSON.stringify({ a: myData }));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable