Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!