BindingProperty is null in handler method called by ajax












1















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.
}









share|improve this question























  • you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

    – John Velasquez
    Jan 3 at 17:34











  • Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

    – Wayn0r
    Jan 3 at 18:02











  • 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
    Jan 3 at 19:01
















1















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.
}









share|improve this question























  • you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

    – John Velasquez
    Jan 3 at 17:34











  • Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

    – Wayn0r
    Jan 3 at 18:02











  • 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
    Jan 3 at 19:01














1












1








1








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.
}









share|improve this question














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.
}






c# asp.net ajax asp.net-core razor-pages






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 17:04









Wayn0rWayn0r

153




153













  • you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

    – John Velasquez
    Jan 3 at 17:34











  • Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

    – Wayn0r
    Jan 3 at 18:02











  • 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
    Jan 3 at 19:01



















  • you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

    – John Velasquez
    Jan 3 at 17:34











  • Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

    – Wayn0r
    Jan 3 at 18:02











  • 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
    Jan 3 at 19:01

















you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

– John Velasquez
Jan 3 at 17:34





you mean you want the instance of Person from OnGet to be access in OnPostAddEntitlement?

– John Velasquez
Jan 3 at 17:34













Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

– Wayn0r
Jan 3 at 18:02





Yep :) The Person is accessable in OnGet and in OnPost at the moment. But not in OnPostAdd but it should be.

– Wayn0r
Jan 3 at 18:02













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
Jan 3 at 19:01





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
Jan 3 at 19:01












1 Answer
1






active

oldest

votes


















2














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






share|improve this answer
























  • Thanks. That worked for me.

    – Wayn0r
    Jan 4 at 9:33











  • glad to help :)

    – John Velasquez
    Jan 4 at 9:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026696%2fbindingproperty-is-null-in-handler-method-called-by-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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






share|improve this answer
























  • Thanks. That worked for me.

    – Wayn0r
    Jan 4 at 9:33











  • glad to help :)

    – John Velasquez
    Jan 4 at 9:33
















2














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






share|improve this answer
























  • Thanks. That worked for me.

    – Wayn0r
    Jan 4 at 9:33











  • glad to help :)

    – John Velasquez
    Jan 4 at 9:33














2












2








2







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 18:14









John VelasquezJohn Velasquez

1,8061613




1,8061613













  • Thanks. That worked for me.

    – Wayn0r
    Jan 4 at 9:33











  • glad to help :)

    – John Velasquez
    Jan 4 at 9:33



















  • Thanks. That worked for me.

    – Wayn0r
    Jan 4 at 9:33











  • glad to help :)

    – John Velasquez
    Jan 4 at 9:33

















Thanks. That worked for me.

– Wayn0r
Jan 4 at 9:33





Thanks. That worked for me.

– Wayn0r
Jan 4 at 9:33













glad to help :)

– John Velasquez
Jan 4 at 9:33





glad to help :)

– John Velasquez
Jan 4 at 9:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026696%2fbindingproperty-is-null-in-handler-method-called-by-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas