I'm checking your demo application Acme.BookStore - in Acme.BookStore.Web -
- the modal is prepared as razor page Pages/Books/CreateModal with get / post method where is:
public async Task<IActionResult> OnPostAsync() { await _bookAppService.CreateAsync(ObjectMapper.Map<CreateBookViewModel, CreateUpdateBookDto>(Book)); return NoContent(); } 2. in index.js I see createModal.onResult method where is only grid refresh.
createModal.onResult(function () { dataTable.ajax.reload(); });
- I want to send created entity Id into onResult js method, but im not sure how to set it even I try it:
- send json object from OnPostSAsync which looks: new JsonResult(new { CategoryId = item.Id } )
- createModal.setResult and createModal is defined as global
- but still I don't know how to access it, because when onPost method ended I need to set setResult property but not sure where
I expected to use "UnobtrusiveJavaScript" approach to return JSON from Post into onSuccess jabvascript method, but it is not fired, even I rewrite Ajax.BeginForm into .net COre version (per source: https://www.aspsnippets.com/Articles/AjaxBeginForm-OnSuccess-not-firing-in-ASPNet-MVC.aspx)
<form class="layui-form" asp-action="CategoryAdd" data-ajax-success="onSuccess" data-ajax="true" data-ajax-method="POST"></form> (per https://dotnetthoughts.net/jquery-unobtrusive-ajax-helpers-in-aspnet-core/)
thanks
2 Answer(s)
-
1
Hi cleverplatform,
Have you ever tried to return object instead of
NoContent()
from modal page:public async Task<IActionResult> OnPostAsync() { var created = await _bookAppService.CreateAsync(ObjectMapper.Map<CreateBookViewModel, CreateUpdateBookDto>(Book)); return new OkObjectResult(created); }
And add parameters to your javascript function to handle that object.
createModal.onResult(function (result, response) { // response.responseText is your returned object. alert(response.responseText.id); dataTable.ajax.reload(); });
-
0
works like charm! Thanks a lot