1

I am using a Html.TextBoxFor helper in an MVC4 project. Our web designer used a custom property of "error-type" for his box that jquery/javascript looks at to determine how an error should be rendered for that textbox.

I tried doing something like:

@Html.TextBoxFor(m => m.SomeValue, new { error-type="blue" });

But C# doesn't like the property name of "error-type".

Is there a way to be able to use the custom property when rendering it through Razor?

1 Answer 1

1

Use an underscore; the helper will convert attribute names containing underscores to dashes. FYI, I believe this will only work with MVC-3 or later.

An alternate route is to use the overload that accepts an IDictionary<string, object> of HTML attributes. That will be both forwards and backwards compatible.

 @Html.TextBoxFor(m => m.SomeValue, new Dictionary<string, object>{{"error-type", "blue"}})
Sign up to request clarification or add additional context in comments.

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.