2

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.

2
  • Why you are creating such a strange API? Why not to pass json object as method parameter ? Commented Sep 20, 2012 at 16:33
  • I am new to programming, do you mean just pass the Data object? Commented Sep 20, 2012 at 16:40

3 Answers 3

3

Try change:

LearnerID: "211"

To (as in model):

Learner_ID: "211"
Sign up to request clarification or add additional context in comments.

3 Comments

@user373721 Your controller is named as model? I am taking about CaseProgress
@user373721 Also I can't find Viewed parameter in action. Please, show error text.
Good points, I moved the action to Home controller, and removed Viewed from the javascript, and added it to the action. Thanks for your comments
1

The problem was in this line:

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

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

Comments

0

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

1 Comment

Thanks, the [HttpPost] was there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.