Setting width of MVC HTML helper DropDownList ! [@HTML.DropDownList]

HTML helpers- MVC, HTML helpers are similar to the ASP.NET webform controls.

HTML helpers are light weight but do not have Events and ViewState unlike ASP.NET controls.

Now, coming back to the topic, it is as easy to set the width for a HTML Helper DropDownList as any plain HTML control.

Signature-

@Html.DropDownList("ListId",Enumerable.Empty<SelectListItem>()new{style="width:250px;"})

  • The first argument is the ListId, any relevant string can be provided here.
  • The Second argument must be an IEnumerable<SelectListItem> type.
  • The third argument can be a new instance of style wherein you can provide style elements like plain HTML.
  • If you want a default value for the DropDownList, then the third argument can be given as default value string and fourth argument as the style element.

Example-

Controller

var exampleList= new List<SelectListItem>();
exampleList.Add(new SelectListItem { Text = "Question1", Value = "1" });
exampleList.Add(new SelectListItem { Text = "Question2", Value = "2" });
exampleList.Add(new SelectListItem { Text = "Question3", Value = "3" });

SelectList selectList = new SelectList(exampleList, "Value", "Text");
ViewBag.SelectList = selectList;

View

@Html.DropDownList("Question1", ViewBag.SelectList as SelectList, "-- Select Question --", new { style = "width: 410px;" })

If you have an external CSS class then the following syntax can be used-

@Html.DropDownList("Question1", ViewBag.SelectList as SelectList, "--Select Question--", new {new { @class="cssClass" })

-where “cssClass” should be the name of the class you want to be reflected to the DropDownList.

Please Like, Comment and Share.

Thanks for Checking out !

Categories
Comments
All comments.
Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. wireless home security systems with cameras and recorders

    Very good article! We are linking to this great post
    onn our site. Keep up the great writing.

    1. Abhimanyu

      Hi,

      I am glad that you found the article helpful.
      Thanks for the feedback 🙂

  2. Spesifikasi Hp

    Undeniably believe that which you stated. Your favorite justification seemed to be on the net the easiest thing to be aware of.
    I say to you, I definitely get irked while people think about worries that they just don’t know about.
    You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal.
    Will probably be back to get more. Thanks

  3. ควย

    This is very interesting, You are a very skilled blogger.
    I’ve joined your rss feed and look forward to seeking more of your fantastic post.

    Also, I have shared your website in my social networks!

  4. real estate commission colorado

    My family members all the time say that I am wasting
    my time here at net, but I know I am getting knowledge all the time by reading thes fastidious posts.

  5. Jasa Konveksi

    Hi! This is my first visit to your blog! We are a group of volunteers and starting
    a new project in a community in the same niche. Your blog
    provided us valuable information to work on. You have done a extraordinary job!

  6. Tamer Uğurluel

    I am curious to find out what blog platform you’re utilizing?
    I’m experiencing some small security problems
    with my latest blog and I’d like to find something more risk-free.
    Do you have any suggestions?

  7. Hp Xiaomi

    Pretty! This has been an extremely wonderful post.
    Thank you for providing these details.

  8. Marita

    I was pretty pleased to uncover this web site. I wanted to thank
    you for your time for this wonderful read!! I definitely loved every little bit of it
    and I have you saved to fav to look at new stuff on your blog.

  9. minecraft kostenlos

    I think this is among the most significant information for me.
    And i am glad reading your article. But want to remark on few general
    things, The website style is great, the articles
    is really excellent : D. Good job, cheers

  10. HuldaQKish

    Pretty section of content. I just stumbled upon your weblog and in accession capital
    to assert that I get actually enjoyed account your blog posts.
    Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.

  11. Abhimanyu

    Sure

  12. Sports.Ru Для Андроид

    These are actually impressive ideas in about blogging. You have touched
    some nice things here. Any way keep up wrinting.

  13. MarlysFGares

    Remarkable issues here. I’m very satisfied to see your article.
    Thanks a lot and I am looking ahead to contact you.
    Will you please drop me a e-mail?