How could I call a Javascript function within the view of my ASP MVC application?
Assign the href value to the anchor tag inside the C# foreach loop. The value will be retrieved from the Javascript function. How can we achieve this functionality in C# the MVC model?
It should get values from the javascript function without any event occurring.
.CSHTML file
@foreach (var item in Model.items)
{
<a href="JavascriptFunction(item.tostring())" class="solutions-grid__card">
Title
</a>
}
Javascript function
function JavascriptFunction(str) {
alert(str);
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
The href tag value should have a Javascript returned value.