【问题标题】:List of JSON objects to controller到控制器的 JSON 对象列表
【发布时间】:2016-04-28 09:55:12
【问题描述】:

我仍在尝试了解如何将 JSON 对象列表发送到控制器以将数据保存在 SQL Server 数据库表“受邀者”中。

举个例子会很有用。

JSON:

[{Email :"e@g.com",Presence :"nodig"},{Email :"h@g.com",Presence :"gewenst"}]

型号:

public class Invitee
{
    public int Id { get; set; }
    public int AppId { get; set; }
    public string Email { get; set; }
    public string Presence { get; set; }
};

控制器:(模型返回空异常,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);
}

jQuery ajax:

$.ajax({
    url: '@Url.Action("InviteeSave", "App")',
    dataType: "json",
    type: "POST",
    accept: 'appication/json; charset=utf-8',
    cache: false,
    data: JSON.stringify(jsonData),
    success: function (data) {
        if (data.success) {
            alert("Met succes !");
        }
    },
    error: function (xhr) {
        alert('error');
    }
});

【问题讨论】:

  • 你的AJAX请求没有问题;问题出在 C# 中。 null 到底是什么?在 Action 中放置一个断点,并在需要时单步执行。
  • appication 中的拼写错误(缺少l
  • 对不起。在控制器“模型”中返回一个 NULL 异常
  • @StephenMuecke 眼睛好:)
  • 使用data: JSON.stringify({ model: jsonData }), - 适合我。

标签: jquery sql-server json ajax asp.net-mvc


【解决方案1】:

抱歉,您不能发送用户定义类型的集合(列表)。 有一个解决方法。

将控制器参数类型设为

C#的DataTable类型

然后通过 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) 
 });

我目前在我的网络应用程序中使用此功能

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

【讨论】:

    【解决方案2】:

    不,您可以将项目集合发送到控制器发布操作方法。

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

    JS: //(url: 应该是 actionname, controller 如果你使用@Url.Action() 或者简单地给出 url: '/controller/method/')

    $.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>
    

    发表你的观点...它对我有用...

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 1970-01-01
      • 2017-06-29
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多