0

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.

2
  • Can you show more code about your view?I cannot see the string packageName. Commented Mar 25, 2022 at 11:37
  • Hm, that is all my code. packageName is meant to be @m.IntegrationPoint from the view (Bad naming practise perhaps). This is the value in the button Commented Mar 25, 2022 at 11:42

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.