1

Using knockout I am trying to send data, from my UI to the controller. This is the javascript used to send my ajax request(PUT)

var model = new Object();
model.StudentID = "";
model.ActiveProgram = "";
model.ProgramDesc = self.programData();
model.Cohorts = self.associationData();
model.LoadIntent = self.loadIntentData();
model.Francophone = self.frenchData();
model.Gender = self.genderData();

    $.ajax({
        url: putStudentRegRequirementsUrl,
        type: "PUT",
        contentType: jsonContentType,
        dataType: "json",
        data: JSON.stringify(model),
        //jsonData:model,
        success: function (data) {
            $('#notificationHost').notificationCenter('addNotification', { message: "Updated.", type: "info" });
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (jqXHR.status != 0)
            {
                $('#notificationHost').notificationCenter('addNotification', { message: "Unable to update registration requirement.", type: "error"});
            }
        }
    });

But when I debug it to see my controller, the string comming in is blank. This is my controller

 [HttpPut]
    public async Task<JsonResult> UpdateRegistrationRequirementAsync(string regRequirementJson)
    {
        try
        {
            var regRequirementModel = JsonConvert.DeserializeObject<RegistrationRequirement>(regRequirementJson);
            var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, regRequirementModel);
            return Json(response);
        }
        catch( Exception ex)
        {
            Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json("Error updating Registration Requirement.");
        }       
    }
3
  • Your Model is an object , and your controller is expecting a string . Commented Jan 28, 2019 at 19:22
  • Sorry, I made a mistake when typing into stack overflow, corrected with sending model as a json Commented Jan 28, 2019 at 19:24
  • Try doing this : UpdateRegistrationRequirementAsync([FromBody]string regRequirementJson) Commented Jan 28, 2019 at 19:40

2 Answers 2

3

Action will parse parameters from client by its name, so you need to pass parameter with name regRequirementJson contains your json. So change this line

data: JSON.stringify(model)

to

data: { regRequirementJson: JSON.stringify(model) }

and remove contentType: jsonContentType.

Or you can try another way. Since ASP.NET can deserialize json by itself you can keep your js code as is and update your controller to

[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement regRequirementModel )
{
    try
    {
        var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, regRequirementModel);
        return Json(response);
    }
    catch( Exception ex)
    {
        Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json("Error updating Registration Requirement.");
    }       
Sign up to request clarification or add additional context in comments.

2 Comments

When trying to call the controller, it doesn't reach this method however anymore.
@Jseb I've updated my answer. And this is good you managed to make the code work.
1

Since you are sending a "RegistrationRequirement" object then in your controller you can do it this way :

[HttpPut]
    public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement registrationRequirement)
    {
        try
        {
            var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, registrationRequirement);
            return Json(response);
        }
        catch( Exception ex)
        {
            Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json("Error updating Registration Requirement.");
        }       
    }

Comments

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.