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

  • Home
  • SEARCH
  • 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 6783261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:50:34+00:00 2026-05-26T16:50:34+00:00

I have this code on controller side to initialise the drop down List<SelectListItem> items

  • 0

I have this code on controller side to initialise the drop down

        List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem
        {Text = "All",
         Value = "All"

        });
        items.Add(new SelectListItem
        {
            Text = "val1",
            Value = "val1"
        });
        items.Add(new SelectListItem
        {
            Text = "val2",
            Value = "val2"
        });

        ViewData["DDLItems"] = items;

        // I have added this code because I am passing the selection back to this     action
        if (Activefilter == null){ Activefilter = "All" ;}

        ViewData["Activefilter"] = Activefilter;

On the view, I have this code

 <%= Html.DropDownList("ActiveFilter", (IEnumerable<SelectListItem>)ViewData["DDLItems"], new { onChange = "func(1)" })%>

for jquery , I have this

$(document).ready(function() {

                      $('#Activefilter').val(<%= ViewData["Activefilter"].ToString() %>);  
           }

so whenever , i change the value of the dropdown, the action is called as shown above. the viewdata[“Actionfilter”] also takes the value selected by the user but when the control comes back to the view,

$('#Filter').val(<%= ViewData["Activefilter"].ToString() %>); 

in this line , it says the value “All” is undefined.

Everytime, the page reloads, I want the dropdown to retain the selected value. can you please help ?

  • 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-26T16:50:34+00:00Added an answer on May 26, 2026 at 4:50 pm

    You can use a SelectList() and in the constructor pass the selected item. Instead of using a list.
    remeber selected

    why is the selected value not being taken in this example,

            List<SelectListItem> items = new List<SelectListItem>();
    
            items.Add(new SelectListItem
            {
                Text = "All",
                Value = "All"
    
            });
            items.Add(new SelectListItem
            {
                Text = "val1",
                Value = "val1",
            });
            items.Add(new SelectListItem
            {
                Text = "val2",
                Value = "val2","
            });
    
    
            string filter = "val1";
            ViewData["DDLItems1"] = new SelectList(items, "Text", "Value", filter);
    

    on view , here is the code

    <div>   @Html.DropDownList("FilterName", (SelectList)ViewData["DDLItems1"])</div>
    

    That is razor syntax, switch to <% notation if you are not using razor

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

Sidebar

Related Questions

I have this controller method: public JsonResult List(int number) { var list = new
I have this code in my controller and want to test this code line
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
hello all i am using this code to show flip animation...... i have a
I have this code in the client side: $.post('<%=Url.Action(Action_Name,Controller_Name)%>', { serverParam: clientParam}, null, null);
I have this code in jQuery, that I want to reimplement with the prototype
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:

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.