无法使用MVC保存JSON数据

时间:2016-04-26 09:56:39

标签: c# jquery json asp.net-mvc

我正在使用MVC4.5而我正在尝试将对象保存到SQL Server数据库中。

我的对象如下:

[{
    "name": "Title",
    "value": "Test"
},{
    "name": "Description",
    "value": "Some text"        
},{
    "name": "Duration",
    "value": "15 minutes"
}]

jquery AJAX是:

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

在控制器中我创建了以下内容:

[HttpPost]
public JsonResult NewAppSave(Appointment model)
{
    if (ModelState.IsValid)
    {
        using (var db = new MainDbContext())
        {
            var appointment = db.Appointments.Create();
            appointment.UserId = 1;
            appointment.Title = model.Title;
            appointment.Description = model.Description;
            appointment.Duration = model.Duration;
            db.Appointments.Add(appointment);
            db.SaveChanges();
        }
    }
    else
    {
        ModelState.AddModelError("", "Something went wrong");
    }
    return Json(model);
}

看起来没有数据发送到控制器。我的代码出了什么问题?

0 个答案:

没有答案