4

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?

2 Answers 2

7

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

@Html.TextBoxFor(model => model.Name, new { ng_model="someModel" });
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. For the last two days i was working on a custom form validator with Request.Form beacuse i wanted to have a clean html markup. And all I had to do is ask a question here and that's it. Thank you very much.
2

It looks like this answer to another question might help you solve your problem. If you're on MVC 3, I feel fairly confident the solution described there should work for you as well. If not, let me know -- my mind's still a little fuzzy this morning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.