Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Send button value with Ajax to Controller in .net core

I'm going crazy trying to figure this one out. First, I'm using entity framework and models etc. So, in my view I'm looping through all my objects, every object has unique properties - one of these properties i want to send to my controller, which does some things, and then reloads the page.

My controller class is as follows:

 public IActionResult Index()
        {
            return View(_context.Modules.ToList());
        }

        [HttpPost]
        public IActionResult LoadModule(string packageName)
        {
            //... do some things here
            return RedirectToAction("Index");
        }

My view is as follows:

<div class="container">
    <div class="row">
        @foreach (var m in Model)
        {
            <div class='col-sm-4'>
                <div class="card border-secondary mb-3">                   
                    <div class="card-body">
                        <div class="row">
                            <button class="btn btn-outline-primary" id="SendValue"
                                type="submit" value="@m.IntegrationPoint">
                                Execute Package
                            </button>
                        </div>
                    </div>
                </div>
                <br />
            </div>
        }
    </div>
</div>

And my Jquery/Ajax:

<script>
    $(document).ready(function() {
    $("#SendValue").click(function(){
        var val = $(this).val();

    $.ajax({
    type:"POST",
    url: '/ExampleModuleController/LoadModule',
    dataType: 'json',
    data:{String:val}
    })
    .fail(function(){alert("fail")})
    .done(function(){alert("success")})

    });
    });
</script>

A link to fiddle for easier view of html.

What am i doing wrong here? It doesn't even seem to register my button click or anything.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Add below code to your view

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Change the url: '/ExampleModuleController/LoadModule',into url: '@Url.Action("LoadModule", "ExampleModule")?IntegrationPoint=' + val ,

Also , in Controller, change the parameter name to string IntegrationPointin LoadModule action.

Note: Because the .fail(function(){alert("fail")}) in the front, so it will alert fail message.

result:

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda