Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Razor Forms - Inserting Angular Attributes

I'm learning C# and Asp.net for about 11 days with background in PHP and PHP related frameworks.

My problem is inserting Angular.js attributes like ng-controller into Razor generated input elements.

The problem is this...

@Html.TextBoxFor(model => model.Name, new { @class = "Form__TextType  Form__TextInput" })

So, this generates an html input[type=text] element with a class attribute. This syntax does not support adding ng-model="someModel" but I can do that with creating a Dictionary that has all the necessary attributes for that element on the server side and putting it as the last argument in TextBoxFor()

That way is not really good although it works. If i want to change an angular attribute, i would have to change the Dictionary object on the server side which could be very error prone.

Is there another way of adding angular attributes to Razor generated elements?

like image 252
Mario Legenda Avatar asked Feb 12 '23 14:02

Mario Legenda


1 Answers

It seems that underscores in the HtmlAttributes parameters are converted to hyphens when rendering:

@Html.TextBoxFor(model => model.Name, new { ng_model="someModel" });
like image 190
Kutyel Avatar answered Feb 20 '23 16:02

Kutyel