Why doesn't onclick work in MVC view?

Coreysan 1,816 Reputation points
2023-12-12T01:38:52.4+00:00

I had a project months ago where I used onclick and it worked great. First I wrote the anchor:

<a type="button" class="btn btn-primary" onclick="showpartial(14, 25, 'Test Product')" data-hint="Quick View"            data-bs-toggle="modal" data-bs-target="#myBox">
    View
</a>


Then I added some Javascript:

<script>
    function showpartial(id, price, name) {
        document.getElementById("myid").innerHTML = id;
        document.getElementById("myprice").innerHTML = price;
        document.getElementById("myname").innerHTML = name;
    }
</script>


and finally I wrote a modal (I'll do just a couple lines):

<div class="modal fade" id="myBox">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            ...
        </div>
    </div>
</div>


But when I copied this code to a new project, it won't work. I click on the button and nothing at all happens.

I'm sure I'm forgetting something I probably added in the original project that's not here.

I'm using Firefox, and there's no debug messages showing errors.

Windows 10

VS 2019

.Net 4.7.2

bootstrap 5.0.1

Any suggestions on what to look for?

Developer technologies | ASP.NET | Other
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.