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
Thx
_Layout.cshtml
Add.cshtml
Add.cshtml.cs
AddItemModel.cs
if you get both, replace :
<input type="submit" value="Save" onclick="buildCatIdList()" />
by :
<input type="submit" value="Save" onclick="buildCatIdList(); return false;" />
<input type="submit" value="Save" onclick="buildCatIdList()"
by :
<input type="submit" value="Save" onclick="buildCatIdList();
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
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");
is supposed to do what?
ASKER
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".
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
ASKER
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(",");
}
ASKER
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
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 :
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 );
}
ASKER
It doesn't pop up any alerts.... seem like statement document.getElementsById has some issues and doesn't perform...
Thx
Thx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works. Great Thx
you welcome!
Open in new window
by :Open in new window
you should get a first alert at page load and a second after you click on the button