Button created Dynamically does not fire Asp-action - core 2 MVC

Asked

Viewed 220 times

0

I have a cshtml (view) on this page I use a typed template and inside I use a POST form, I create a div(dinamicamnete with ajax) with some components, ALL appear normal, but the button to fire an action in the controller does not work, If I leave statico the same shoots and calls the action. C# . net Core 2.1 with MVC and Bootstrap

class ReimpressaoView {

constructor(elemento) {

    this._elemento = elemento;
}
_template(model) {
    return ` <button asp-action="ConfirmaReimpressaoCertificadoPDF" type="submit" class="btn btn-primary">
                      Imprimir
                     </button>
             <div class="jumbotron">
               <div class="container">
                  <p class="text-justify" style="font-size:medium">O R.G. Pesquisado é Referente ao certificado emitido em <span class="badge badge-pill badge-primary" style="float:center"> ${model.dataEmissaoAntecedentes} </span></p>
                  <p class="text-justify" style="font-size:medium">Com o código <span class="badge badge-pill badge-primary" style="float:center">${model.codigoUnico}</span></p>
               <hr />
                  <p class="lead" style="font-size:medium"><strong>Obs:</strong>Este certificado tem validade até a data de <span class="badge badge-pill badge-primary" style="float:center"> ${model._dataEmissaoAntecedentes}</span></p>
              </div>  
        `;
}

update(model) {
    this._elemento.innerHTML = this._template(model);
}

AJAX

enviarRgUsuarioCertificado(event) {
        event.preventDefault();
             if (this._inputrgReimpressao.value === "") {
            alert('Favor preencher o campo RG!!');
            return;        }
     let model = { RG: this._inputrgReimpressao.value }
        $.ajax({
            url: '/certificado/GetusuarioAntecedentesRG',
            type: 'Post',
            contentType: 'application/json',
            data: JSON.stringify(model),
            async: false,
            success: function (response) {
                 dataemissao = response.dataEmissaoAntecedentes;
                 codigoUnico = response.codigoUnico;             
            }
        });
     }

1 answer

0


This won’t work because you are writing a component of Razor, which should be interpreted on the server side before rendering the view, in your ready html and on the user screen.

<button asp-action="ConfirmaReimpressaoCertificadoPDF" type="submit" class="btn btn-primary">
  Imprimir
</button>

You need to write the already rendered element, if you want to add it dynamically via javascript.

What would be something like:

<button type="submit" formaction="/SuaController/ConfirmaReimpressaoCertificadoPDF" class="btn btn-primary" >
   Imprimir
</button>
  • Thank you so much! It worked Perfectly!

  • @Thiagobarros If the proposal is the answer to your question, it is important to vote as useful and accept as an answer, so your question and the solution can be useful to other users who might face the same problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.