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 8062689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:45:21+00:00 2026-06-05T10:45:21+00:00

This is my Model: public class MyModel{ public string Name { get; set; }

  • 0

This is my Model:

public class MyModel{
 public string Name { get; set; }
 public string listType { get; set; }
 public string SelectedItem { get; set; }
}

I have a partial view:

@model List<MyModel>
@{

SelectListItem iEqualTo = new SelectListItem { Text = "Equel To", Value = "EqualTo" };
SelectListItem iNotEqualTo = new SelectListItem { Text = "Not Equal To", Value = "NotEqualTo" };
SelectListItem iGreaterThan = new SelectListItem { Text = "Greater Than", Value = "GreaterThan" };
SelectListItem iLessThan = new SelectListItem { Text = "Less Than", Value = "LessThan" };
SelectListItem iBetween = new SelectListItem { Text = "Between", Value = "Between" };


List<SelectListItem> MyFirstList = new List<SelectListItem>() {
    iEqualTo, iNotEqualTo, iGreaterThan, iLessThan, iBetween
};

List<SelectListItem> MySecondList = new List<SelectListItem>() {
    iEqualTo, iNotEqualTo
};

}


@foreach(var item in Model) {

  if(item.listType =="Firsttype"){

   <span> @Html.DropDownList(item.Name , MyFirstList) </span>

  } else {

   <span> @Html.DropDownList(item.Name , MySecondList) </span>

  }
}

So I need Selected = true in List<SelectListItem> in DropDownList that item which name equal to MyModel.SelectedItem? what is your suggestion? In actual my view have more SelectListItem and List<SelectListItem> and for some limitations I can’t pass the List<SelectListItem> as my model property and I need to handle it in view. I think to handle it by lambda inline but I don’t know is possible, something like this: @Html.DropDownList(item.Name , MySecondList.each(a=> if (a.ToString() == item.SelectedItem) {a.Selected = true})

Is any way to do this? and if I am wrong to use inline lambda does any other way to handle this?

  • 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-06-05T10:45:24+00:00Added an answer on June 5, 2026 at 10:45 am

    You can use a SelectList, which has a constructor with an IEnumerable and an Object (selected value).

    @Html.DropDownList(item.Name, new SelectList(MyFirstList, item.SelectedItem));
    

    EDIT

    I think I faced your problem when trying to use a DropDownList in a loop.

    You can try to use this generic extension method (in a static “helper” class)

    public static IEnumerable<SelectListItem> ToSelectListItem<T, TValue, TText, TSelectedValue>(this IEnumerable<T> enumerable,
                Func<T, TText> text,
                Func<T, TValue> value,
                TSelectedValue selectedValue)
            {
                return enumerable.Select(item => new SelectListItem
                {
                    Text = text(item).ToString(),
                    Value = value(item).ToString(),
                    Selected = value(item).Equals(selectedValue)
                }).AsEnumerable();
            }
    

    and then

    @Html.DropDownList(item.Name, MyFirstList.ToSelectListItem(m => m.Text, m => m.Value, item.SelectedItem)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is My model: public class MyModel { public List<long> NeededIds { get; set;
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a view which displays a model eg: public class MyModel() { public
I have this Model public class SalesModelView { [Key] public int SaleId { get;
public class User { public int Id {get;set;} public string Name {get;set} public ICollection<User>
I have a class Similar to this public class Model { public TimeSpan Time1
I'm using MVC3 and I have a model like this: public class Foo {
I have a model that looks something like this: public abstract class Parent {
I have on my model: public class EmailTemplateModel { public int EmailTemplateId { get;
public class Person { private int _Id; public int Id{get{return value;} set{_Id=value;}} private string

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.