hncl
hncl

Reputation: 2295

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).
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:

   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.

Upvotes: 2

Views: 1373

Answers (3)

hncl
hncl

Reputation: 2295

The problem was in this line:

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

I removed it and it worked. Thanks for all answers.

Upvotes: 1

vittore
vittore

Reputation: 17579

In addition to answers above, check that you have [HttpPost] attribute applied to your action.

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

Try change:

LearnerID: "211"

To (as in model):

Learner_ID: "211"

Upvotes: 3

Related Questions