TheDizzle
TheDizzle

Reputation: 1574

Add class to @html.DropDownListFor

I have a few dropdown models that I need to add the bootstrap form-control class to. I am struggling converting this string to accept the class.

I'd appreciate the help.

I've put

, new { @class = "form-control"} 

everywhere in the line to no success.

 @Html.DropDownListFor(model => model.SelectedSourceAccount,  new 
SelectList(Model.AllWithdrawalAccounts.Select(x => new {Value = x.Id, Text = 
x.NameAndAvailableBalance}), "Value", "Text"), Model.SelectedSourceAccount)

Upvotes: 0

Views: 95

Answers (3)

Suraj Shrestha
Suraj Shrestha

Reputation: 1808

As there are multiple overloaded method for dropdown, I think you've used a wrong overloaded method. You can use a correct overloaded method provided.

Upvotes: 0

Arun Kumar
Arun Kumar

Reputation: 957

@Html.DropDownListFor(model => model.SelectedSourceAccount, new 
SelectList(Model.AllWithdrawalAccounts.Select(x => new {Value = x.Id, Text = 
x.NameAndAvailableBalance}),string.Empty, new { @class = "dropdown" })

Upvotes: 0

user3559349
user3559349

Reputation:

Try (note you do not need the .Select(..) when creating the select list)

@Html.DropDownListFor(model => model.SelectedSourceAccount, new SelectList(Model.AllWithdrawalAccounts, "Id", "NameAndAvailableBalance"), new { @class = "form-control"})

Upvotes: 1

Related Questions