SignalR + MVC: "TypeError: can't access property "value", event.target.attributes['data-id'] is undefined"

Tiger Ser 1 Reputation point
2022-11-30T02:37:00.523+00:00

Hi, I am newbie recently taking a tutorial on SignaR with Asp MVC, I encountered a bug in voting.js that I could not able to solve it by myself, here is error:

Javascript Debugger
(Idk why it cant get "data-id" from .cshtml display code T.T)

This is my VotingHub:

enter image description here

My .cshtml display code:

enter image description here

And here is voting function:

   csharp  
   public int Vote(int id, bool direction)  
       {  
           var item = GetByID(id);  
           if (item == Character.NotFound) return 0;  
                    
           System.Console.WriteLine($"Voting for {item.Name}");  
     
           if (direction) item.Votes++;  
           else item.Votes--;  
             
           _ctx.Characters.Update(item);  
           _ctx.SaveChanges();  
     
           return item.Votes;  
       }  

What i try wanna do is wire up voting but it seem my signalR code couldn't connect through, all other feature of app work just fine except this voting feature, i hope some one could give me some instruction or point me to somewhere to learn so i able to understand it.

Much appreciated your time for help !

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-11-30T07:28:36.373+00:00

    Hi @Tiger Ser ,

    265559-image.png

    TypeError: can't access property "value", event.target.attributes['data-id'] is undefined"

    About this issue, try to find the custom attribute via the votingButtons, instead of the event.

    Code like this:

    let itemId = votingButtons[i].attributes["data-id"].value;  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

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