Json对象

时间:2017-05-11 19:04:55

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

因此,当我设置javascript ReminderModel对象属性时,这将起作用,如果我将LeadIdStaffId设置为1,我的控制器将接收该对象,但是只要我将它们转换为字符串控制器收到一个空对象。

我不确定发生了什么,引号可能有问题?

我的观看代码

var ReminderModel = {
                        "LeadId": "@Model.Id",
                        "StaffId": "1a53c286-b17e-4afd-9ccb-684fd1a92f7b",
                        "Subject": "Call Back",
                        "Time": "iTime",
                        "Date": "iDate"
                    };


                    $.ajax({
                        url: "@Url.Action("SetReminder","Appointment")",
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(ReminderModel),
                        success: function (valid) {
                            if (valid) {
                                //show that id is valid
                            } else {
                                //show that id is not valid
                            }
                        }
                    });

我的控制器方法

[HttpPost]
        public async Task<ActionResult> SetReminder([FromBody] ReminderViewModel viewModel)
        {

            using (var db = new LeadCoreDbContext())
            {
                DateTime dt = DateTime.MinValue;
                DateTime.TryParse($"{viewModel.Date} {viewModel.Time}", out dt);

                if (dt == DateTime.MinValue)
                    throw new Exception("Failed to set reminder");

                var reminder = new Reminder
                {
                    Deleted = false,
                    LeadId = viewModel.LeadId,
                    Subject = viewModel.Subject,
                    TimeToRemind = dt,
                    StaffId = viewModel.StaffId
                };

                db.Reminder.Add(reminder);
                await db.SaveChangesAsync();

            }

            return Content("received");
        }

这是模型

public class ReminderViewModel
{
    public int Id { get; set; }
    public int LeadId { get; set; }
    public int StaffId { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
    public string Subject { get; set; }
}

0 个答案:

没有答案