user3265569
user3265569

Reputation: 191

Saving data with json in custom class

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.

Upvotes: 0

Views: 198

Answers (2)

Gene R
Gene R

Reputation: 3744

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.

Upvotes: 2

Mainul
Mainul

Reputation: 889

Add [HttpPost] attribute on the method body like

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

Upvotes: 0

Related Questions