Home > Software design >  Is it possible to update only one property of a model using AJAX?
Is it possible to update only one property of a model using AJAX?

Time:01-25

I have a model, JobApplicationForm. This model is split into a short app and a long app. Meaning the 1st page posts certain attributes to the model, and once the last 3 pages are finished another post updates the model to be completely filled out.

public class JobApplicationForm
{
    public int Prop1 {get; set}
    public string Prop2 {get; set;}
    public string Prop3 {get; set;}
    public string Prop4 {get; set;}
    public string Prop5 {get; set;}
    public int CompletionStatus {get; set;} 
}

In the 1st Post action, the model fills Prop1 and Prop2, and sets CompletionStatus = 1. This is a post after 1 page of the form is completed.

Then in the 2nd Post action, there are 3 more pages that fill in Prop 3, 4, 5, and sets CompletionStatus = 4 when the final page is submittmed.

Since the 2nd part of the form is 3 pages that come together. Is there a way to use AJAX to simply update completionStatus to 2 and 3 on their respective pages? When trying to post using AJAX I have found online that the model has to be fully filled out, and am unable to find anyone just updating one property. I am not trying to update the view at all when changing completionStatus, just simply trying to update the model's property.

Ajax(status is an int variable defined elsewhere)

    $('.next.button').click(function() {
    $.post('/Home/UpdateCompletionStatus', {completionStatus : status});
});

3rd Controller I created to update this one property.

[HttpPost]
public ActionResult UpdateCompletionStatus([FromBody]JobApplicationForm model, int status)
{
    if (ModelState.IsValid)
    {
        model.CompletionStatus = status;
    }
    return new EmptyResult();
}

CodePudding user response:

I solved my own problem. The reason it wasn't updating was because the model binding is confused. I called the paramater status in the controller but then in the ajax called it completionStatus.

  •  Tags:  
  • Related