Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:37:27+00:00 2026-05-26T05:37:27+00:00

So I’m trying to stylize the tag in a dropdown. My MVC helper is

  • 0

So I’m trying to stylize the tag in a dropdown. My MVC helper is set up kind of like this but the real thing pulls from the DB, so the data is not hard coded.

@{
    List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
    List<string> ListOfValue = new List<string> { "1","2","3"};
    List<SelectListItems> ListOFSELCETLISTITEMS = new List<SelectListItem>();    

    for (int x = 0; x < 3; x++)
    {
        ListOFSELCETLISTITEMS .Add(new SelectListItem
        {
            Text = ListOfText[x],
            Value = ListOfValue[x],
            Selected = (selectedValue == ListOfValue[x])
        });
    }
}   
@Html.DropDown("NAME",ListOFSELCETLISTITEMS)

This gives me something like

<select id="Name" name="Name">
    <option value="1">FirstThing</option>
    <option value="2">SecondThing</option>
    <option value="3">ThirdThing</option>
</select>

What i need is something like this

<select id="Name" name="Name">
    <option value="1" class="option1">FirstThing</option>
    <option value="2" class="option2">SecondThing</option>
    <option value="3" class="option3">ThirdThing</option>
</select>

I’ve tried doing things like

@{
    List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
    List<string> ListOfValue = new List<string> { "1","2","3"};
}
<select id="Name" name="Name">
    @ for (int x = 0; x < 3; x++)
    {
        <text>
        <option value="@ListOfValue[x]" @{if(selectedValue == ListOfValue[x])
            { @Html.Raw("selected='selected'") }}
            @class = '@Html.Raw("option"+x)'>
            @ListOfText[x]
        </option>
        </text>
    }   
</select>

But this seems to confuse the controller and it doesn’t recognize that the dropdown’s value above maps to

public ActionResult method(string Name)

in the controller on a post or get. Well the line

 @Html.DropDown("NAME",ListOFSELCETLISTITEMS)

in the view does allow the controller to understand the method should map to that dropdown’s value.

How can i do this? Is there a way to unconfuse the controller and be able to hand write HTML without the Html.helpers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:37:27+00:00Added an answer on May 26, 2026 at 5:37 am

    To render directly a controller action, you can try :

    @{ Html.RenderAction(Action, Controller); }

    To invoke your controller action that should be returning a string with your content.

    [HttpGet]
    public string Action(int id) 
    {
       return your context in here
    }
    

    However I think it’s cleaner to add an Ajax action returning the data for building the select and after having the results, using jquery solution for adding a class (which can be returned in the AJAX response itself)

    EDIT: clarification:

    Assuming you’ve got a collection of Item as follows:

    class Item {
       public int Value { get; set; }
       public string CssClass { get; set; }
       public string Description{ get; set; }
    }
    private const string EMPTY_OPTION = "<option value=''></option>";
    [HttpGet]
    public string Action(int id) 
    {
        // Load a collection with all your option's related data
        IQueryable data = LoadSomethingFromDbOrWherever(id);
        // Build output
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("<select id='foo' name='foo'>");
        sb.AppendFormat(EMPTY_OPTION);
        foreach (Item b in data)
        {
                sb.AppendFormat("<option value='{0}' class='{1}'>{2}</option>",
                                b.Value, b.CssClass, b.Description);
        }
        sb.AppendFormat("</select>");
        return sb.ToString();
    }
    

    for ajax option:

    [HttpGet]
    public JsonResult Action(int id) 
    {
       //same as above, obtain a collection
        // Load a collection with all your option's related data
        IQueryable data = LoadSomethingFromDbOrWherever(id);
        var jsonData = new
                {
                        from c in data
                        select new
                        {
                            Value= c.Value,
                            CssClass = c.CssClass,
                            Description = c.Desription
                        }).ToArray()
                };
        return Json(jsonData);
    }
    

    Call this via $.ajax and in the callback you’ll have a javascript object containing all your data, then use jQuery to build up the select options.

    Regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.