Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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
Jquery 控制器的JSON对象列表_Jquery_Sql Server_Json_Ajax_Asp.net Mvc - Fatal编程技术网

Jquery 控制器的JSON对象列表

Jquery 控制器的JSON对象列表,jquery,sql-server,json,ajax,asp.net-mvc,Jquery,Sql Server,Json,Ajax,Asp.net Mvc,我仍在试图理解如何将JSON对象列表发送到控制器,以将数据保存在SQL server数据库表“Invitees”中 举个例子会非常有用 JSON: 型号: public class Invitee { public int Id { get; set; } public int AppId { get; set; } public string Email { get; set; } public string Presence { get; set; } };

我仍在试图理解如何将JSON对象列表发送到控制器,以将数据保存在SQL server数据库表“Invitees”中

举个例子会非常有用

JSON:

型号:

public class Invitee
{
    public int Id { get; set; }
    public int AppId { get; set; }
    public string Email { get; set; }
    public string Presence { get; set; }
};
控制器:模型返回一个空的exeption,ModelState=true

[HttpPost]
public JsonResult InviteeSave(List<Invitee> model)
{
    if (ModelState.IsValid)
    {
        using (var db = new MainDbContext())
        {
            var dbInv = db.Invitees.Create();

            foreach (var inv in model)
            {
                dbInv.AppId = 35;
                dbInv.Email = inv.Email;
                dbInv.Presence = inv.Presence;

                db.Invitees.Add(dbInv);
            }

            db.SaveChanges();
        }
    }
    else
    {
        ModelState.AddModelError("", "Something went wrong");
    }
    return Json(model);
}

很抱歉,您无法发送用户定义类型的CollectionList。 有一个工作机会

将控制器参数类型设置为

C的数据表类型

然后通过ajax发布数据。它对我有用

$.ajax({
        type: "POST",
        url: "URL/api/.....",
        data: {Email :"e@g.com",Presence :"nodig"},{Email :"h@g.com",Presence :"gewenst"},
    }).done(function( data ){ 
         console.log(data) 
 });
我目前在我的web应用程序中使用此功能

编辑:
您可以发布列表;控制器参数的字符串列表。然后,您可以对数据进行相应的排序。

不,您可以将项目集合发送给控制器post action方法

[HttpPost]
public JsonResult InviteeSave(List<Invitee> model)
{
    //Your code goes here
}
我的观点是

<form id="frmInvitee" action="javascript:void(0)">
    <input type="submit" name="BtnSumbit" id="BtnSumbit" />
</form>

发表你的观点。。。它适合我…

您的AJAX请求没有问题;问题是C语言。什么是null?在操作中放置断点,并在需要时单步执行。应用程序中的拼写错误缺少该错误的原因。在控制器中,“模型”返回空值exception@StephenMuecke好眼力:使用data:JSON.stringify{model:jsonData},-对我来说很好。
[HttpPost]
public JsonResult InviteeSave(List<Invitee> model)
{
    //Your code goes here
}
$.ajax({
            url: "InviteeSave",
            dataType: "json",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            cache: false,
            data: JSON.stringify({ model: jsonData }),
            success: function(data) {
                if (data.success) {
                    alert("Met succes !");
                }
            },
            error: function(xhr) {
                alert('error' + 'Came here');
            }
        });
<form id="frmInvitee" action="javascript:void(0)">
    <input type="submit" name="BtnSumbit" id="BtnSumbit" />
</form>