0

I am having a strange issue with ASP core and Javascript and could not find any answer out there.

The index page of the web site I am working with, receives from the controller, a ViewModel containing among other things two attributes. One named "testID" and one named "test". Both have exactly the same information, a string which is an ID.

C#:

public IActionResult Index()
        {
            InboxViewModel newViewModel = new InboxViewModel();

            newViewModel.TestID = "sdf-sdf-sdf-sdf-sdf-sdf-sdf";
            newViewModel.test = "sdf-sdf-sdf-sdf-sdf-sdf-sdf";

            return View(newViewModel);
        }

Now, on my Razor page I set an onClickTest() method onto an tag:

<a href="#" onclick="onClickTest();"> Click Here </a>

The function in Javascript looks like this:

JAVASCRIPT:

function onClickTest() {

       ViewModelJS = @Html.Raw(Json.Serialize(Model)); //Translate C# Model to JS Model.
       
       console.log('test id: ' + ViewModelJS.testid); // This print as UNDEFINED
       console.log('test   : ' + ViewModelJS.test);   // This one prints fine.

}

Like you can see from the above code (Javascript), the line that prints the "test" attribute works as supposed but the attribute named "testid", does not. Prints "UNDEFINED".

Then, if I take away the "id" part of the name or change it to some other thing (without the word "id" on it), it works fine! So it must has something to do with the name...

Now, if the site is running and I open the browser console and print manually something like "console.log(ViewModelJS.testid)" then it works fine, as it should be. But not when I click on the tag and the function is called.

Any ideas???

Thanks a lot!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Luca Prodan
  • 75
  • 1
  • 5
  • "TestID" != "testid" – Crowcoder Apr 06 '21 at 17:37
  • How is it not case sensitive? If you use `ViewModelJS.TEST` that one still works? What is the runtime value of `ViewModelJS`? Lastly, what is the point of that anyway? If you want to access the model make them html elements, even if they are Hidden inputs. – Crowcoder Apr 06 '21 at 17:45
  • @LucaProdan You can use the information in [How to list the properties of a JavaScript object?](https://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) to find out if it is there and what its actual name is. – Andrew Morton Apr 06 '21 at 17:47
  • @LucaProdan it *might* be due to protection-level and/or serialization of the `TestID` attribute? - see https://stackoverflow.com/a/33726793/12511801 – Marco Aurelio Fernandez Reyes Apr 06 '21 at 17:49
  • You know what, you were right. For some reason the Json.Serialize() function change all the names from the standard Pascal case in C# to camel case in Javascript.. That was the reason I could not assign properly... Like that TestID transformed into testID in the Javascript version... Thanks man!!! – Luca Prodan Apr 06 '21 at 17:53
  • Thanks to all of you guys!!! – Luca Prodan Apr 06 '21 at 17:56
  • @Crowcoder, that´s a great option (the HTML elements) I just did not want to populate the page with hidden tags.. But great option indeed.. – Luca Prodan Apr 06 '21 at 17:59

0 Answers0