简体   繁体   English

ASP.NET MVC JSON实体框架 - 保存数据

[英]ASP.NET MVC JSON Entity Framework - Saving data

I have an asp.net MVC3 application, trying to save data to MS SQL table (Entity Framework). 我有一个asp.net MVC3应用程序,试图将数据保存到MS SQL表(实体框架)。
Here is the table: 这是表格:

public class CasesProgress
    {
        public virtual long ID { get; set; }
        public virtual long Learner_ID { get; set; }
        public virtual long Course_ID { get; set; }
        public virtual long StudyCase_ID { get; set; }
        public virtual long CaseList_ID { get; set; }
        public virtual bool Viewed { get; set; }
    }

Here is my controller: 这是我的控制器:

public ActionResult StoreProgress(long Learner_ID, long Course_ID, long StudyCase_ID, long CaseList_ID)
    {
        CasesProgress casesprogress = new CasesProgress();
        casesprogress.Learner_ID = Learner_ID;
        casesprogress.Course_ID = Course_ID;
        casesprogress.StudyCase_ID = StudyCase_ID;
        casesprogress.CaseList_ID = CaseList_ID;
        casesprogress.Viewed = true;
        db.CasesProgresses.AddObject(casesprogress);
        db.SaveChanges();
        return Json(new { success = true });
    }

and here is my Javascript: 这是我的Javascript:

   function StoreProgress1() {
        $.ajax({
            url: '/Home/StoreProgress',
            type: 'POST',
            data: {
                LearnerID: "211",
                Course_ID: "6",
                StudyCase_ID: "19",
                CaseList_ID: "2"
            },
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    }

I get error message without even going to the breakpoint I have in the controller. 我得到错误消息,甚至没有去控制器中的断点。 Any idea, I am new to this. 任何想法,我是新来的。 Thanks in advance. 提前致谢。

Try change: 尝试更改:

LearnerID: "211"

To (as in model): 至(如模型中):

Learner_ID: "211"

The problem was in this line: 问题出在这一行:

contentType: 'application/json; contentType:'application / json; charset=utf-8', 字符集= UTF-8' ,

I removed it and it worked. 我删除它,它工作。 Thanks for all answers. 谢谢你的所有答案。

除上述答案外,请检查是否已将[HttpPost]属性应用于您的操作。

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

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