0

I'm still trying to understand this ajax.

I'm trying to save the Patient from js to the database on the serverside, but the code below always result in [alert("error")]

Can someone see the mistake?

MVC action:

public JsonResult CreatePatient(string patient)
{
    var jsonPatient = JsonConvert.DeserializeObject<Patient>(patient);

    if (db.Patients.Contains(jsonPatient))
    {
        db.Patients.Remove(jsonPatient);
    }
    db.Patients.Add(jsonPatient);

    return new JsonResult();
}

Custom class:

public class Patient
{
    [Key]
    public string Cpr { get; set; }         //ID
    private string _firstname;
    private string _lastname;

    //public List<TestReceving> TestHandelings { get; set; }

    public string Firstname
    {
        get { return _firstname; }
        set { _firstname = value; }
    }

    public string Lastname
    {
        get { return _lastname; }
        set { _lastname = value; }
    }
    public override bool Equals(object obj)
    {
        return obj is Patient ? Cpr == (obj as Patient).Cpr : false;
    }
}

js:

function savePatient() {
    var Patient = {
        Cpr: $("#cpr").val(),
        Lastname: $("#lastname").val(),
        Firstname: $("#firstname").val()
    };

    var dataToPost = JSON.stringify(Patient);

    $.ajax({
        type: "POST",
        url: "/Patient/CreatePatient",
        contentType: "application/json; charset=utf-8",
        data: dataToPost,
        dataType: "json",
        success: function () {
            // do what you want on success.
            alert("saved");
        },
        error: function () {
            alert("error");
        }
    });
}

I have changed it to:

public JsonResult CreatePatient(Patient patient)
{
    if (db.Patients.Contains(patient))
    {
        db.Patients.Remove(patient);
    }
    db.Patients.Add(patient);

    return new JsonResult();
}

and

function savePatient() {
    var Patient = {
        Cpr: $("#cpr").val(),
        Lastname: $("#lastname").val(),
        Firstname: $("#firstname").val()
    };

    $.ajax({
        type: "POST",
        url: "/Patient/CreatePatient",
        contentType: "application/json; charset=utf-8",
        data: Patient,
        dataType: "json",
        success: function () {
            // do what you want on success.
            alert("saved");
        },
        error: function () {
            alert("error");
        }
    });
}

But I am still getting the error.

2
  • public JsonResult CreatePatient(Patient patient) Commented Feb 12, 2016 at 9:47
  • Hi. I tried what you said, but still gets the error. Will you take a look again? Commented Feb 12, 2016 at 10:11

2 Answers 2

2

You dont need to JSON.stringify, just send Patient as is:

data: Patient,

and recieve in action:

public JsonResult CreatePatient(Patient patient)
{
    ...
}

Update: while sending raw json (not stringified) contentType: "application/json" should not be used.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I still get the error. I have edited the question. Will you take a look at it again?
@user3265569 does your action getting fired? if yes then u have some exception in action, try to debug it
I am getting: "typeName": "System.ArgumentException", "message": "Invalid JSON primitive: Cpr.",
@user3265569 try remove contentType: "application/json; charset=utf-8"
Thank you for your help.
0

Add [HttpPost] attribute on the method body like

[HttpPost]
public JsonResult CreatePatient(Patient patient){...}

1 Comment

I'm gonna mark "Gene R"'s as solved I think it was a combination of both thougt. Thansk for your response.

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.