Open Closed

Send info about created entity ID(json) from modal window to parent grid using modal.setResult #1479


User avatar
0
cleverplatform created

I'm checking your demo application Acme.BookStore - in Acme.BookStore.Web -

  1. 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(); });

  1. 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)
  • User Avatar
    1
    enisn created
    Support Team .NET Developer

    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(); 
    });
    
  • User Avatar
    0
    cleverplatform created

    works like charm! Thanks a lot

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38