Mohamoud Mohamed
Mohamoud Mohamed

Reputation: 525

Adding bootstrap classes with html helper methods not working?

Is there any way to add bootstrap classes like form-control to asp.net mvc html helper methods like @html.Textbox("searchString");?

this is the code I have in the view

@this is the html output for the search bar taking searchstring param from controller in htmlhelper@

@using (Html.BeginForm())
{<div class="jumbotron">
        <div class="row">
            <div class="col-md-12">
            <p>

                Search: @Html.TextBox("SearchString")
                <input class="btn btn-info" type="submit" value="Search" />

            </p>
            </div>
        </div>
    </div>
}

It seems like everything is playing nice with bootstrap except the @Html.Textbox and its messing up my UI. is there a way to affect the @Html.Textbox with bootstrap form-control class?

ps the "SearchString" variable comes from my controller.

Upvotes: 1

Views: 4024

Answers (3)

qJake
qJake

Reputation: 17139

One variation of the @Html.TextBox helper takes an anonymous object that you can use to pass in HTML attributes. You would use it like:

@Html.TextBox("SearchString", null, new { @class = "form-control" })

Upvotes: 0

Razvan Dumitru
Razvan Dumitru

Reputation: 12452

Yes.

You have the possibility to add HtmlAttributes:

@Html.TextBox("SearchString", null, new { @class = "form-control" }) 

And if you're using a property from your @model:

@Html.TextBoxFor(m => m.Property, new { @class = "form-control" })  

Upvotes: 1

paul
paul

Reputation: 22001

@Html.TextBox("SearchString", null, new {@class="form-control"});

Upvotes: 1

Related Questions