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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:46:58+00:00 2026-05-17T18:46:58+00:00

Im using asp.net mvc 2. I have a model Supermodel that consists of 2

  • 0

Im using asp.net mvc 2. I have a model Supermodel that consists of 2 models TestModel1 and TestModel2.
In SuperModelView Im doing the following thing:

 <%: Html.DisplayFor(x=> x.TestModel1, "TestModel1Template") %>

Its working just fine, except for the fact, that dropdownlist is populated but selected value is not set.
Im using the following code for a dropdownlist in my template:

<%: Html.DropDownListFor(x=> x.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.Property1) %>

and its not setting the selected property. I put the code below to SuperModelView, that calls <%: Html.DisplayFor
To populate the template and it works just fine. So I`m kinda puzzled, what is the difference?

<%: Html.DropDownListFor(x=> x.TestModel1.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.TestModel1.Property1) %>

UPDATE: I`ve tried to investigate the issue, but something is totally wrong. I can share the whole code, not sure where to put it, here or attach with separate files.

@Darin, what other parts should I share, or just share the whole model view and controller files?

  • 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-17T18:46:58+00:00Added an answer on May 17, 2026 at 6:46 pm

    Firstly display templates are just for displaying. If you need to edit with drop down use editor template:

    <%: Html.EditorFor(x => x.TestModel1, "TestModel1Template") %>
    

    and in your editor template:

    <%: Html.DropDownListFor(x => x.Property1, Model.MyDDLList) %>
    

    where MyDDLList is defined like:

    public IEnumerable<SelectListItem> MyDDLList { get; set; }
    

    and in your controller action you fill the values:

    public ActionResult Foo() 
    {
        var model = new SuperViewModel 
        {
            TestModel1 = new TestModel1
            {
                // Set some selected value
                Property1 = "1",
    
                // Fill the drop down values
                // TODO: use a repository
                MyDDLList = new SelectList(new[]
                {
                    new SelectListItem { Value = "1", Text = "text 1" },
                    new SelectListItem { Value = "2", Text = "text 2" },
                    new SelectListItem { Value = "3", Text = "text 3" },
                }, "Value", "Text")
            }
        }
        return View(model);
    }
    

    UPDATE:

    Here’s a complete working example:

    Model:

    public class MyViewModel
    {
        public string SelectedItemId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    Controller:

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                // Preselect the second item
                SelectedItemId = "2",
                Items = new SelectList(new[]
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                }, "Value", "Text")
            };
            return View(model);
        }
    }
    

    View (~/Views/Index.aspx):

    <%: Html.DisplayForModel() %>
    

    DisplayTemplate (~/Views/DisplayTemplates/MyViewModel):

    <%: Html.DropDownListFor(x => x.SelectedItemId, Model.Items) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using ASP.NET MVC, I have a Model, to which I'm attaching attributes so that
I am using asp.net MVC to develop an application that will have ajax interactions.
I have an ASP.NET MVC (Beta 1) website that I'm using themes with. When
I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0
Anyone have any experience creating a modal dialog box using AJAX and ASP.NET MVC?
I am in the start up of a project using ASP.NET MVC and have
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
I've been developing a site using ASP.NET MVC, and have decided to use the
I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question:
Just getting started using MVC in ASP.NET, I'm going to have it so users

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.