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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:07:41+00:00 2026-05-27T03:07:41+00:00

In my project the DropDownListFor(x => x code sits in an EditorTemplate. It is

  • 0

In my project the DropDownListFor(x => x code sits in an EditorTemplate. It is used to populate a table of data one of the fields of the table is the drop down list. Whilst everything is rendering with no issues, the drop down list does not default to the pre-selected item I am settings in the ViewModel. What am I not seeing?

Code as follows:

ViewModel:

public class FooDetailViewModel : ViewModelBase
{
    public List<FooPermissionObject> FooPermissions { get; set; }
}

The Strongly-typed model object:

public class FooPermissionObject
{
    public string Name { get; set; }
    public int Reason { get; set; }
    public IEnumerable<SelectListItem> Reasons { get; set; }
    public bool Selected { get; set; }
}

The controller:

var viewModel = new StockLineManagementDetailViewModel();

using (_model)
{
    foreach (var company in _model.GetAllRecords<Company>())
    {
        var permissionModel = new FooPermissionObject
        {
            Name = company.Name,
            Selected = true,
            Reasons = _model.GetAllRecords<FooPermissionReason>()
                    .ToList()
                    .Select(x => new SelectListItem
                    {
                        Value = x.FooPermissionReasonId.ToString(),
                        Text = x.FooPermissionReasonDesc
                    }),
            Reason = record.FooPermissionReasonId
        };

        viewModel.FooPermissions.Add(permissionModel);
   }
}

The View:

<table id="myTable" class="tablesorter" style="width:98%">
    <thead>
         <tr>
        <th>
            Name
        </th>
        <th>
            Excluded
        </th>
        <th>
            Reason for Exclusion
        </th>
       </tr>
    </thead>
    <tbody>
        @Html.EditorFor(x => x.FooPermissions)
    </tbody>
</table>

The EditorTemplate:

@model FooPermissionObject
<tr>
    <td>
        @Html.DisplayFor(x => x.Name, new { @readonly = "readonly"})
        @Html.HiddenFor(x => x.Name)
    </td>
    <td>
        @Html.CheckBoxFor(x => x.Selected)
    </td>
    <td>
        @Html.DropDownListFor(x => x.Reason, Model.Reasons)
    </td>
</tr>

Anyone got any ideas why this wouldn’t populate the DropDownListFor with the Object represented by the Reason value from the Reasons collection?

  • 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-27T03:07:42+00:00Added an answer on May 27, 2026 at 3:07 am

    I can’t see any code where you are setting selected = true in your select list. You are setting the Selected property of your FooPermissionObject but that is irrelevant your drop down list is bound to the reasons collection. You want something like this:

    .Select(x => new SelectListItem
    {
        Value = x.FooPermissionReasonId.ToString(),
        Text = x.FooPermissionReasonDesc,
        Selected = (Some codition or other)
    })
    

    Replacing some condition or other with whatever your criteria is to say which item should be selected.

    EDIT:

    A better way might be as below:

    Reasons = new SelectList(_model.GetAllRecords<FooPermissionReason>(),
                             "FooPermissionReasonId",
                             "FooPermissionReasonDesc",
                             record.FooPermissionReasonId)
    

    The params to the constructor of SelectList are: Collection to bind, value field, text field, selected value.

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

Sidebar

Related Questions

Project: Take Wikipedia's list of Roman consuls, put the data in a CSV so
Project: ASP.NET 3.5 with C# I have this much :- A table which used
Project Darkstar was the topic of the monthly JavaSIG meeting down at the Google
The Project's Web section (under project properties in VS2008) has a list of debuggers:
Project is C#. So I have a bunch of multithreaded code that is designed
Project Scenario Technology : Dotnetnuke (Approx. 100 - 150 screens) Data Architecture : LINQ
Project Lombok makes it trivial to implement the boilerplate code in the classes. Is
Project background aside, I've implemented a table of custom JComboBoxes. Each row of ComboBoxes
I've got a view that outputs a table of projects and each project has
Project : ASP.NET 3.5 Server : SQL Server 2008 One of the page in

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.