Link to home
Start Free TrialLog in
Avatar of Simon Leung
Simon Leung

asked on

Cannot run JavaScript on ASP.NET Core

I find that my Javascript inside ASP.NET Core Razor page can't be called up. Any idea ?

Thx
_Layout.cshtml
Add.cshtml
Add.cshtml.cs
AddItemModel.cs
Avatar of leakim971
leakim971
Flag of Guadeloupe image

replace :
    <script type="text/javascript">
    function buildCatIdList() {
        const categories = document.getElementsByName("Category");

Open in new window

by :
    <script type="text/javascript">
alert("one");
    function buildCatIdList() {
alert("two");
        const categories = document.getElementsByName("Category");

Open in new window


you should get a first alert at page load and a second after you click on the button
if you get both, replace :
<input type="submit" value="Save" onclick="buildCatIdList()" />
by :
<input type="submit" value="Save" onclick="buildCatIdList();return false;" />
Avatar of Simon Leung
Simon Leung

ASKER

It seems that the JavaScript can run, as it pops up the alert windows.

However, the Javascript code cannot read the selected categories and put into a variable Categories.

Any idea ?

Thx again.
Error01.png
Error02.png
document.getElementsByName("Category");

Open in new window


is supposed to do what?
Hi,

It may get a list of categories for a product. When users click on each category box, it will be checked in the Javascript and join together in a hidden field "AIM_Categories".
could you add an alert like this :

const categories = document.getElementsByName("Category");
alert(categories.length); // and let me know what number you see in the alert please

Open in new window

5
try the following and come back to comment what you get, thanks :

function buildCatIdList() {
    const categories = document.getElementsByName("Category");
    const catIds = [];
    alert(categories.length); // you said you got 5 in the alert
    for (var i = 0; i< categories.length; i++)
    {
        var cat = categories[i];
        alert("cat.checked is: " + cat.checked + "\ncat.id is: " + cat.id);
        if (cat.checked) {
            catIds.push(cat.id.split("-")[1]);
        }
    }
    alert(catIds); // please do a screen shot of it and post it on EE
    document.getElementsById("<%= AIM_Categories.ClientID %>").value = catIds.join(",");
}

Open in new window

seem like there is a data binding issue in form element AIM_Categories to the model class Categories in AddItemModel.cs...

Thx again.
Screenshot01.png
Screenshot02.png
ok so CatIds is fine
the last part is to be sure your element is here in the page :

function buildCatIdList() {
    const categories = document.getElementsByName("Category");
    const catIds = [];
    alert(categories.length); // you said you got 5 in the alert
    for (var i = 0; i< categories.length; i++)
    {
        var cat = categories[i];
        if (cat.checked) {
            catIds.push(cat.id.split("-")[1]);
        }
    }
    alert("AIM Cat is " + (document.getElementsById("<%= AIM_Categories.ClientID %>")?"":"not") + " present");
    document.getElementsById("<%= AIM_Categories.ClientID %>").value = catIds.join(",");
    alert("its value is set to: " + document.getElementsById("<%= AIM_Categories.ClientID %>").value );
}

Open in new window

It doesn't pop up any alerts.... seem like statement document.getElementsById has some issues and doesn't perform...

Thx
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works. Great Thx
you welcome!