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 !
Latest posts by Abhimanyu (see all)
- Switch between multiple proxies in Chrome browser [ProxySwitchy Guide] - February 12, 2016
- CISCO VPN CLIENT -REASON 442: FAILED TO ENABLE VIRTUAL ADAPTER [Fix] - January 31, 2016
- CynogenMod 13 (Android Marshmallow [6.0]) on Samsung Galaxy S3 GT-I9300. [21-12-2015] - December 21, 2015
Very good article! We are linking to this great post
onn our site. Keep up the great writing.
Hi,
I am glad that you found the article helpful.
Thanks for the feedback 🙂
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
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!
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.
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!
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?
Pretty! This has been an extremely wonderful post.
Thank you for providing these details.
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.
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
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.
Sure
These are actually impressive ideas in about blogging. You have touched
some nice things here. Any way keep up wrinting.
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?