1

Well here I'm trying to add a Bootstrap class to the DropDownList generated by Razor, but it keeps giving me squigglies.

 @Html.DropDownList("NationalityId", String.Empty, new {@class="form-control"})

Any recommendation as to how I can get this to work.

4
  • Where do you get the squigglies? What is the error message? Commented Dec 26, 2013 at 10:32
  • 1
    You are missing two braces at the end: } ) Commented Dec 26, 2013 at 10:34
  • 1
    Sorry the braces are a typo here, syntactically the code is correct, it's probably not the correct overload. Commented Dec 26, 2013 at 10:36
  • 1
    pass null instead of string.empty, my guess is that you hit wrong overload. Commented Dec 26, 2013 at 10:39

3 Answers 3

3

it works:

@Html.DropDownList("NationalityId",new List<SelectListItem>(), new {@class="form-control"})

Result html:

<select name="NationalityId" id="NationalityId" class="form-control"></select>
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain the syntax, does the NationalityId get passed to the correct table in this case. What does String.empty do anyway?
Could you please explain what is the resulting HTML in my code? This seems to work and render the correct class though. Thanks
@Html.DropDownList generates Result html, just use correct overload. name="NationalityId" - using for correct binding
2

Try this one:

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

2 Comments

Does String.Empty and null render the same HTML
No, but String.Empty was the bug in the line of code, if I am not mistaken. You tried to pass a string where a list was expected. NULL or better yet an empty list (see the other answers) should solve the problem.
0

Another option:

@Html.DropDownList("NationalityId", Enumerable.Empty<SelectListItem>(),new {@class="form-control"})

Comments

Your Answer

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