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

0

I have a cshtml (view) on this page I use a typed model and inside I use a POST form, I create a div (dynamically with ajax) with some components, ALL appear normal, but the button triggers an action on 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;             
            }
        });
     }
    
asked by anonymous 27.09.2018 / 14:50

1 answer

0

This will not work because you are writing a Razor component, which should be interpreted from the server side before rendering the view, in your ready html and on the user's screen.

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

You need to write the element already rendered, 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>
    
27.09.2018 / 15:47