Event click javascript does not call the action - Asp.net Core

Asked

Viewed 102 times

0

I created a modal block to display my View Create. When you click on btnNovo, a javascript event should call my Action Create to return a blank view and click inside the modal... Only the modal is opening. I followed with BP and found that the click event is not calling the Action Create with the route I am passing. Can anyone tell me where I’m going wrong?

 [HttpGet]
        [Authorize(Policy = "CanWritePessoaSituacaoData")]
        [Route("situacoes-gerenciamento/cadastrar-novo")]
        public IActionResult Create()
        {
            return View();
        }

Below is the btnNovo code, the modal block and the ref. to the script:

 <a id="btnNovo" class="btn btn-outline btn-default new" data-toggle="modal" href="#myModal"
                   data-original-title="Cadastrar Novo" >
                    <span title="Cadastrar Novo" class="icon wb-plus"></span> Cadastrar Novo
                </a>
                
     

<div id="myModal" class="modal fade" role="dialog" data-backdrop="static" tabindex='-1' aria-labelledby="myModalLabel" aria-hidden="true" style="display:none" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h4 class="modal-title" id="myModalLabel">Gerenciar Situações de Pessoas</h4>
            </div>
            <div class="modal-body">
                <div id="modal-content">
                    Carregando...
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    
    <script src="~/js/PessoaSituacao/PessoaSituacao.js"></script>
    <script type="text/javascript">
    
    }

Follow the js script:

$("btnNovo").click(function (eve) {
    $("#modal-content").load("situacoes-gerenciamento/cadastrar-novo");
});

  • What is the controller name and route for it? the path in the load is incomplete $("#modal-content").load("/{controller}/situacoes-gerenciamento/cadastrar-novo");

  • Personal Controller Action Create.

  • 1

    Did you try? $("#modal-content").load("/PesssoaSituacao/situacoes-gerenciamento/cadastrar-novo"); ?

  • I had tried, but I think I was wrong to pass a wrong route... Now yes, it worked!!! Thanks Leandro!

1 answer

0


The problem is in your note in the button click event

$("btnNovo").click(function (eve) {
    $("#modal-content").load("situacoes-gerenciamento/cadastrar-novo");
});

Here you are indicating only the route of your action from the current route which includes the current action. As reported in your comments Controller is called PessoaSituacaoController and does not have a route customization for the same, so by default it would be PessoaSituacao. Taking these notes into account, to solve your problem just indicate the url for loading the modal respecting the standard /{Controller}/{Action ou Rota}/, that in your case would look like this:

$("btnNovo").click(function (eve) {
    $("#modal-content").load("/pessoaSituacao/situacoes-gerenciamento/cadastrar-novo");
});

Browser other questions tagged

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