简体   繁体   English

Ajax POST 到 Asp.net MVC

[英]Ajax POST to Asp.net MVC

I want to post with AJAX to store data in database and I'm using ADO.Net.我想使用 AJAX 发布以将数据存储在数据库中,并且我正在使用 ADO.Net。 However, it doesn't working.但是,它不起作用。

Here my code:这是我的代码:

Controller控制器

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

Model模型

    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 AJAX POST

And this ajax in .js.还有这个 .js 中的 ajax。 Not in views不在视图中

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";
}

and here the error这里是错误

System.InvalidOperationException: 'The entity type ScoresQ is not part of the model for the current context.' System.InvalidOperationException: '实体类型 ScoresQ 不是当前上下文模型的一部分。'

As error shows problem is in the code where you are adding object into db.由于错误显示问题出在您将对象添加到数据库的代码中。 share that code for further analyze.共享该代码以供进一步分析。

Your model you are sending from JQuery doesnt meet model in Controller.您从 JQuery 发送的模型与控制器中的模型不符。 Try to modify it in this way:尝试以这种方式修改它:

 $.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");
            }
    });

I already fix the error, I just change the ajax code like this我已经修复了错误,我只是像这样更改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");
            }
    });

You are passing variables separately in ajax, you need to pass as an object,您在ajax中单独传递变量,您需要作为对象传递,

Try the below code,试试下面的代码,

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");
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM