实体框架类上的POST请求获取"无效的JSON原语"错误

时间:2015-01-16 20:05:56

标签: jquery asp.net-mvc json entity-framework

我正在尝试返回我的Entity Framework生成的模型类的JSON对象。以下是我的观点:

@{
    ViewBag.Title = "Clock In/Out";
    var currentEmployee = ViewBag.CurrentEmployee;
    var existingTimeEntry = ViewBag.ExistingTimeEntry;
    var btnValue = "";
    var btnClass = "";

    btnValue = currentEmployee.IsClockedIn == true ? "Clock Out" : "Clock In";
    btnClass = currentEmployee.IsClockedIn == true ? "clock-out-btn" : "clock-in-btn";
}

<header class="login-header">
    <div class="clearfix">
        <h1>@ViewBag.Title</h1>
    </div>
</header>
@{
    using (Html.BeginForm(null, null, FormMethod.Post, new { id = "clock-in-form" }))
    {
        <div class="clock-in-wrapper">
            <p class="text-center">You are currently logged in as @currentEmployee.FirstName @currentEmployee.LastName.</p>
            <input type="hidden" id="EmployeeID" name="EmployeeID" value="@currentEmployee.EmployeeID"/>
            <div class="row">
                <div class="col-md-12">
                    <input type="submit" value="@btnValue" class="btn btn-primary @btnClass" />
                </div>
            </div>
        </div>
     }
}

这是我的控制器:

// POST: TimeEntries/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
public ActionResult Create([Bind(Include = "TimeEntryID,EmployeeID,TimeStart,TimeEnd,IsClosed")] TimeEntry timeEntry) {

    var currentUserId = User.Identity.GetUserId();
    var currentUser = db.AspNetUsers.FirstOrDefault(c => c.Id == currentUserId);
    var currentEmployee = db.Employees.FirstOrDefault(c => c.EmployeeID == currentUser.EmployeeID);
    ViewBag.CurrentEmployee = currentEmployee;
    var existingTimeEntry = db.TimeEntries.FirstOrDefault(te => te.EmployeeID == currentEmployee.EmployeeID && te.IsClosed == false);
    ViewBag.ExistingTimeEntry = existingTimeEntry;
    var timeEntryID = -1;

    if (existingTimeEntry != null)
    {
        timeEntryID = existingTimeEntry.TimeEntryID;
    }

    ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName");

    if (ModelState.IsValid)
    {
        if (timeEntryID <= 0)
        {
            // Grab the current employee and set clocked in to true.
            var employee = db.Employees.Find(timeEntry.EmployeeID);
            employee.IsClockedIn = true;

            // Create the time entry.
            timeEntry.TimeStart = DateTime.Now;
            timeEntry.IsClosed = false;
            db.TimeEntries.Add(timeEntry);

            ViewBag.CurrentTimeEntry = timeEntry;

            db.SaveChanges();

            return Json(timeEntry, JsonRequestBehavior.AllowGet);
        }
        else {
            var updatedTimeEntry = db.TimeEntries.Find(timeEntryID);
            var employee = db.Employees.Find(timeEntry.EmployeeID);

            employee.IsClockedIn = false;
            updatedTimeEntry.TimeEnd = DateTime.Now;
            updatedTimeEntry.IsClosed = true;

            db.SaveChanges();

            return Json(updatedTimeEntry, JsonRequestBehavior.AllowGet);
        }
    }

    ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName", timeEntry.EmployeeID);
    return View(timeEntry);
}

这是我的jQuery:

$(function () {
    $("#clock-in-form").on("submit", function (e) {
        e.preventDefault();
        var submitButton = $("#clock-in-form input[type='submit']");
        var dt = new Date();
        var formData = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "/TimeEntries/Create",
            data: JSON.stringify(formData),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                if (submitButton.hasClass("clock-in-btn")) {
                    submitButton.removeClass("clock-in-btn");
                    submitButton.addClass("clock-out-btn");
                    submitButton.attr("value", "Clock Out");
                    console.log(data);
                    $(".clock-in-alerts").append('<div class="alert alert-success text-center">You have successfully logged in at ' + data.TimeStart + '.</div>');
                } else if (submitButton.hasClass("clock-out-btn")) {
                    submitButton.removeClass("clock-out-btn");
                    submitButton.addClass("clock-in-btn");
                    submitButton.attr("value", "Clock In");
                }
            },
            error: function (xhr, status, error) {
                console.log(xhr);
                console.log(status);
                console.log(error);
            }
        });
    });
});

当我尝试提交表单时,我收到内部服务器错误(500),并在响应文本中显示“无效的JSON原语:EmployeeID。”

1 个答案:

答案 0 :(得分:0)

设置contentType: "application/json",请求正文将被视为JSON内容。这就是"Invalid JSON primitive"错误发生的原因,因为URL编码格式与JSON格式不同。

删除:

contentType: "application/json; charset=utf-8",