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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:25:43+00:00 2026-06-07T17:25:43+00:00

Is there any way to say my view model property should be rendered as

  • 0

Is there any way to say my view model property should be rendered as DropDownList (so that I can specify DropDownList items)?

I have found a lot of custom implementations but I guess there should be a built-in way to implement such a basic thing.

Update. I am rendering my model by Html.EditorForModel method, I don’t want to use methods like Html.DropDownListFor

  • 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-07T17:25:47+00:00Added an answer on June 7, 2026 at 5:25 pm

    There’s no built-in template which renders a dropdown list, except for the Nullable<bool> type which renders a Not Set, Yes, No dropdown but I assume that’s not what you are asking about.

    So let’s build one. As always we start by defining the view model that will represent a dropdown containing 2 properties (one for the selected value and one for the available values):

    public class ItemViewModel
    {
        public string SelectedId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    then we could have a standard view model with this property:

    public class MyViewModel
    {
        public ItemViewModel Item { get; set; }
    }
    

    then a controller that will fill the view model:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Item = new ItemViewModel
                {
                    SelectedId = "2",
                    Items = new[]
                    {
                        new SelectListItem { Value = "1", Text = "item 1" },
                        new SelectListItem { Value = "2", Text = "item 2" },
                        new SelectListItem { Value = "3", Text = "item 3" },
                    }
                }
            };
            return View(model);
        }
    }
    

    and a corresponding view (~/Views/Home/Index.cshtml):

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
    }
    

    Now all that’s left is to define a custom editor template for the DropDownViewModel type (~/Views/Shared/EditorTemplates/DropDownViewModel.cshtml):

    @model DropDownViewModel
    @Html.DropDownListFor(
        x => x.SelectedId, 
        new SelectList(Model.Items, "Value", "Text", Model.SelectedId)
    )
    

    and override the default template for the Object type in order to allow Deep Dive as Brad Wilson explains in his blog post. Otherwise by default ASP.NET MVC won’t recurse into complex subtypes for your templates. So we override ~/Views/Shared/EditorTemplates/Object.cshtml:

    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
    {
        if (prop.HideSurroundingHtml) 
        {
            @Html.Editor(prop.PropertyName)
        } 
        else 
        { 
            <div class="editor-label">
                @(prop.IsRequired ? "*" : "")
                @Html.Label(prop.PropertyName)
            </div>
            <div class="editor-field">
                @Html.Editor(prop.PropertyName)
                @Html.ValidationMessage(prop.PropertyName, "*")
            </div>
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a picture 1000x1000. Is there any way to set a background
Let's say I have 10 threads running simultaneously. Is there any way of calling
Let's say you have var funct=function(a,b){ return a+b; }; console.log(funct); Is there any way
Is there any way I can set a formatter on models that will convert
Is there any way to get PDF document text language? Example: Let's say I
Is there a way to take any number, from say, 1 to 40000 and
We have a model (say, List<string> ). The function that builds the list is
Is there anyway that an indexed array say ['1' => 'dave, '2' => 'ryan',
Is there anyway to make a secondary key in SQL? Lets say I have
Is there any way in Notepad++ (or even with another tool) to change the

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.