3

I've just downloaded VS 2013 because its new native support to the Bootstrap framework.

I create my forms, but they look different to original bootstrap ones. Problem is that it seems as razor and its "EditorFor" method, is not adding the "form-control" class needed for bootstrap.

I could add it as an html parameter, but then I would have to do that for each and every text field in my site.

How can I fix/modify Razor for it to automatically add this class for this objects?

1
  • 1
    Visual Studio itself makes little difference to what you're asking. Its the MVC libraries/helpers that matter. AFAIK they don't "natively support bootstrap".. because that would mean that every time you used EditorFor it would add unwanted classes to controls if you weren't using bootstrap. Commented Apr 10, 2014 at 3:44

3 Answers 3

2

Try using TextBoxFor.

@Html.TextBoxFor(model => model.Title, new {style="border:1px solid red;"})

This works. and if you want to add any class

@Html.TextBoxFor(model => model.Title, new {@class="fooo"})

should work.

Sign up to request clarification or add additional context in comments.

2 Comments

It work, but only with TextBoxFor if I use the default EditorFor it does not work. Which Means that I would have to replace it all over my app...
Yeah, as you mentioned, they promise to get editorfor work in mvc 5.1 but didnt quite well. Better if you go with this. It is proven to work.
1

HTML classes must always be added manually. Nothing will do this for you automatically by default.

There are a few options for you:

1 Comment

You seem to be right, I've just found this asp.net/mvc/overview/releases/mvc51-release-notes#Bootstrap but anyways I can't get it to work even "adding" the class as they said, it just not get added.
-1

At the end as definitely it's not an error, it's just that .NET decided not to include this. http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Bootstrap

The easiest way I foudn was using Jquery and adding the class to all the field that I need to in my case all the text-boxes en select lists.

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

<script>
    $(".text-box").addClass("form-control");
    $("select").addClass("form-control");
</script>
}

1 Comment

This seems too messy to me. I would suggest not using razor and adding the class as plain html. For me, this creates greater flexibility if I want to add a framework such as angular to the form.

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.