C # Razor Forms - Inserting Angular Attributes

I have been learning C # and Asp.net for about 11 days with background in PHP and PHP related frames.

My problem is inserting Angular.js attributes like ng-controller

in input elements generated by Razor.

The problem is that...

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

      

So this generates an html element input[type=text]

with a class attribute. This syntax does not support append ng-model="someModel"

, but I can do it with creating a dictionary that has all the required attributes for that element on the server side and puts it as the last argument inTextBoxFor()

This method is not very good, although it works. If I want to change the agular attribute, I will have to change the Server side Dictionary object, which can be very error prone.

Is there any other way to add angular attributes to Razor generated elements?

+3


source to share


2 answers


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



@Html.TextBoxFor(model => model.Name, new { ng_model="someModel" });

      

+5


source


It looks like this answer to another question might help you solve your problem. If you are on MVC 3, I'm sure the described solution should work for you too. If not, let me know - my mind is still a little fuzzy this morning.



+2


source







All Articles