1
votes

In a Razor page I want to change the data of an entity. The data is loaded in OnGet and saved in OnPost. The data that is loaded in OnGet is saved to a property called Person. At a later time I can simply retrieve them in OnPost (it's the identical object).

However, if I use a handler called by an Ajax call, the property object is initialized (integer properties are 0, object properties are zero) but it is not the original object anymore.

What do I have to do so that the original object is also available in the handler called by the Ajax call?

I already tried to use [BindProperty] attribute and to use a hidden input in the razor page. Or to access ViewData.Model But is does not work. Person and other data of the model is still kind of null.

Ajax-Call:

function addEntitlement() {
        var vacationEntitlement = {};
        vacationEntitlement["Year"] = $('#newEntitlementYear').val();
        vacationEntitlement["Days"] = $('#newEntitlementDays').val();
        vacationEntitlement["PersonID"] = $('#hiddenPersonID').val();
        $.ajax({
            type: "POST",
            url: "./Edit?handler=AddEntitlement",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("XSRF-TOKEN",
                    $('input:hidden[name="__RequestVerificationToken"]').val());
            },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(vacationEntitlement)
        }).fail(function () {
            alert('error');
        }).done(function () {
        });
    }

PageModel:

    public IActionResult OnGet(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        Person = _unitOfWork.PersonRepository.GetByID(id);

        if (Person == null)
        {
            return NotFound();
        }

        return Page();
    }

    public JsonResult OnPostAddEntitlement([FromBody] VacationEntitlement vacationEntitlement)
    {
      ///Tried to acces Person or ViewData.Model.Person here.
      ///But Person is just intialized but does not contain the expected data.
    }
1
you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement? - John Velasquez
Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be. - Wayn0r
That's not how things work. HTTP is a stateless protocol, so each request is as if it's the first time the client has ever interacted with the server. If you want some data to stick around, you need to create a session and save it there. That could be via Session or TempData. TempData is just a specialized form of Session that removes values after they're accessed, whereas normal session state would persist for the life of the session. - Chris Pratt

1 Answers

2
votes

Try using TempData it allows you to pass data from one action to another action

Person = _unitOfWork.PersonRepository.GetByID(id);
TempData["Person"] = Person

then

public JsonResult OnPostAddEntitlement([FromBody] VacationEntitlement vacationEntitlement)
{
   if(TempData.ContainsKey("Person")) {
     var person = TempData["Person"] as Person; /* (as Person} I just assume the class name is Person */
     // place your logic here
   }
}

and also make sure to properly setup your TempData configuration https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2#tempdata