Ajax POST 到 Asp.net MVC

Ajax POST to Asp.net MVC

我想 post 和 AJAX 将数据存储在数据库中,我正在使用 ADO.Net。但是,它不起作用。

这是我的代码:

控制器

 [HttpPost]
        public JsonResult InsertScore(ScoresQ scores)
        {
            db.ScoresQs.Add(scores);
            db.SaveChanges();
           
         
           return Json("true", JsonRequestBehavior.AllowGet);
        }

型号

    public partial class ScoresQ
    {   [Key]
        public int id { get; set; }
        private DateTime? month = null;
        public DateTime DateCreated
        {
            get
            {
                return this.month.HasValue
                   ? this.month.Value
                   : DateTime.Now;
            }

            set { this.month = value; }
        }
        public Nullable<int> status { get; set; }
        public Nullable<int> score { get; set; }
      
    }

AJAX POST

还有 .js 中的 ajax。不在视图中

function showScore(data) {
    quiz.style.display = "none";
    scoreBlock.style.display = "block";
    scoreBlock.innerHTML = "<p> You scored " + score + " out of " + data.length + "</p>";
    console.log(score);
    if (score < 4) {
        scoreMessage.innerHTML = "<p>Not so good! Time for some revision.</p>";
    }
    else if (score < 8) {
        scoreMessage.innerHTML = "<p>Pretty good! But still room for improvement.</p>"
    }
    else {
        scoreMessage.innerHTML = "<p>Great work! You really know your birds!</p>"
    }

        $.ajax({
            type: "post",
            url: "Home/InsertScore",
            dataType: "json",
            data: { score: score, status: 1},
            success: function (data) {
                console.log("Success");
            },
            error: function () {
                console.log("error");
            }
    });
    scoreMessage.style.display = "block";
    quizAgain.style.display = "block";
}

这里是错误

System.InvalidOperationException: 'The entity type ScoresQ is not part of the model for the current context.'

如错误所示,问题出在您将对象添加到数据库的代码中。分享该代码以供进一步分析。

您从 JQuery 发送的模型不符合 Controller 中的模型。 尝试这样修改:

 $.ajax({
            type: "post",
            url: "Home/InsertScore",
            dataType: "json",
            data: {model.month: month, model.DateCreated: date, model.score: score, model.status: 1},
            success: function (data) {
                console.log("Success");
            },
            error: function () {
                console.log("error");
            }
    });

我已经修复了错误,我只是像这样更改 ajax 代码

  $.ajax({
            type: "post",
            url: "Home/InsertScore",
            dataType: "json",
            data:
            {"score": score, "status": 1},        
            success: function (data) {
                console.log("Success");
            },
            error: function () {
                console.log("error");
            }
    });

您在ajax中单独传递变量,您需要作为对象传递,

试试下面的代码,

var scores = {score: score, status: 1};
$.ajax({
        type: "post",
        url: "Home/InsertScore",
        dataType: "json",
        data: scores ,
        success: function (data) {
            console.log("Success");
        },
        error: function () {
            console.log("error");
        }
});