MVC5 Razor html.dropdownlistfor set selected when value is in array

VansFannel

I'm developing an ASP.NET MVC 5 application, with C# and .NET Framework 4.6.1.

I have this View:

@model MyProject.Web.API.Models.AggregationLevelConfViewModel

[...]

@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, (SelectList)Model.HelperCodeTypeItems, new { id = "Configurations[0].HelperCodeType" })

The ViewModel is:

public class AggregationLevelConfViewModel
{
    private readonly List<GenericIdNameType> codeTypes;
    private readonly List<GenericIdNameType> helperCodeTypes;

    public IEnumerable<SelectListItem> CodeTypeItems
    {
        get { return new SelectList(codeTypes, "Id", "Name"); }
    }

    public IEnumerable<SelectListItem> HelperCodeTypeItems
    {
        get { return new SelectList(helperCodeTypes, "Id", "Name"); }
    }

    public int ProductionOrderId { get; set; }

    public string ProductionOrderName { get; set; }

    public IList<Models.AggregationLevelConfiguration> Configurations { get; set; }

    public AggregationLevelConfViewModel()
    {
        // Load CodeTypes to show it as a DropDownList
        byte[] values = (byte[])Enum.GetValues(typeof(CodeTypes));

        codeTypes = new List<GenericIdNameType>();
        helperCodeTypes = new List<GenericIdNameType>();

        for (int i = 0; i < values.Length; i++)
        {
            GenericIdNameType cType = new GenericIdNameType()
            {
                Id = values[i].ToString(),
                Name = EnumHelper.GetDescription((CodeTypes)values[i])
            };

            if (((CodeTypes)values[i]) != CodeTypes.NotUsed)
                codeTypes.Add(cType);

            helperCodeTypes.Add(cType);
        }
    }
}

And Models.AggregationLevelConfiguration is:

public class AggregationLevelConfiguration
{
    public byte AggregationLevelConfigurationId { get; set; }
    public int ProductionOrderId { get; set; }
    public string Name { get; set; }
    public byte CodeType { get; set; }
    public byte HelperCodeType { get; set; }
    public int PkgRatio { get; set; }
    public int RemainingCodes { get; set; }
}

I need to set selected value in these properties:

public IEnumerable<SelectListItem> CodeTypeItems
{
    get { return new SelectList(codeTypes, "Id", "Name"); }
}

public IEnumerable<SelectListItem> HelperCodeTypeItems
{
    get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}

But I can't set it in new SelectList(codeTypes, "Id", "Name"); or new SelectList(helperCodeTypes, "Id", "Name"); because the selected value are in Configurations array: fields AggregationLevelConfiguration.CodeType and AggregationLevelConfiguration.HelperCodeType.

I think I have to set selected value in the View, but I don't know how to do it.

How can I set the selected values?

user3559349

Unfortunately @Html.DropDownListFor() behaves a little differently than other helpers when rendering controls in a loop. This has been previously reported as an issue on CodePlex (not sure if its a bug or just a limitation)

The are 2 option to solve this to ensure the correct option is selected based on the model property

Option 1 (using an EditorTemplate)

Create a custom EditorTemplate for the type in the collection. Create a partial in /Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml (note the name must match the name of the type

@model yourAssembly.AggregationLevelConfiguration
@Html.DropDownListFor(m => m.HelperCodeType, (SelectList)ViewData["CodeTypeItems"])
.... // other properties of AggregationLevelConfiguration

and then in the main view, pass the SelectList to the EditorTemplate as additionalViewData

@using (Html.BeginForm())
{
  ...
  @Html.EditorFor(m => m.Configurations , new { CodeTypeItems = Model.CodeTypeItems })
  ...

Option 2 (generate a new SelectList in each iteration and set the selectedValue)

In this option your property CodeTypeItems should to be IEnumerable<GenericIdNameType>, not a SelectList (or just make codeTypes a public property). Then in the main view

@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.CodeTypeItems, "Id", "Name", Model.Configurations[0].HelperCodeType)

Side note: there is no need to use new { id = "Configurations[0].HelperCodeType" - the DropDownListFor() method already generated that id attribute

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Java

Html.DropdownListFor selected value not being set

From Dev

Html.DropDownListFor set selected value

From Dev

Set selected value for DropDownListFor in controller does not work in razor view

From Dev

How to set Default value in MVC 4 razor DropDownListFor

From Dev

How to set default selected value for Html.DropDownListFor

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

ASP.NET MVC @Html.DropdownListFor wrong selected value

From Dev

MVC DropDownListFor selected value not being selected

From Dev

Using selected value in @Html.DropDownListFor

From Dev

Unable to get @Html.DropDownListFor Selected Value

From Dev

Html.DropDownListFor gives error if no value selected

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

Selected value for dropdownlistfor in mvc4

From Dev

@Html.DropDownListFor how to set default value

From Dev

dropdownlistfor selected value with viewdata

From Dev

DropDownListFor selected value not working

From Dev

MVC5 / Razor TextAreaFor null value

From Dev

MVC - DropDownListFor Enum - Set Default Value

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Html.Textbox helper not setting the value of textbox in MVC5 Razor

From Dev

Default selected value in mvc5 dropdown

From Dev

MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

From Dev

Kendo MVC DropDownListFor does not bind selected value to model

From Dev

DropDownListFor not always showing selected value

From Dev

'dropdownlistfor' controller not taking the selected value

From Dev

How to set value into HTML5 date field from Asp.Net MVC Razor?

From Dev

MVC 5 Html helper CheckBoxFor cannot be selected in Razor partial

Related Related

  1. 1

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  2. 2

    Html.DropdownListFor selected value not being set

  3. 3

    Html.DropDownListFor set selected value

  4. 4

    Set selected value for DropDownListFor in controller does not work in razor view

  5. 5

    How to set Default value in MVC 4 razor DropDownListFor

  6. 6

    How to set default selected value for Html.DropDownListFor

  7. 7

    MVC DropdownlistFor set selected value not working?c#

  8. 8

    ASP.NET MVC @Html.DropdownListFor wrong selected value

  9. 9

    MVC DropDownListFor selected value not being selected

  10. 10

    Using selected value in @Html.DropDownListFor

  11. 11

    Unable to get @Html.DropDownListFor Selected Value

  12. 12

    Html.DropDownListFor gives error if no value selected

  13. 13

    MVC DropDownListFor - Selected Value ViewModel Class

  14. 14

    Selected value for dropdownlistfor in mvc4

  15. 15

    @Html.DropDownListFor how to set default value

  16. 16

    dropdownlistfor selected value with viewdata

  17. 17

    DropDownListFor selected value not working

  18. 18

    MVC5 / Razor TextAreaFor null value

  19. 19

    MVC - DropDownListFor Enum - Set Default Value

  20. 20

    Set @Html.Dropdownlistfor to session in MVC 4

  21. 21

    Set @Html.Dropdownlistfor to session in MVC 4

  22. 22

    Html.Textbox helper not setting the value of textbox in MVC5 Razor

  23. 23

    Default selected value in mvc5 dropdown

  24. 24

    MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

  25. 25

    Kendo MVC DropDownListFor does not bind selected value to model

  26. 26

    DropDownListFor not always showing selected value

  27. 27

    'dropdownlistfor' controller not taking the selected value

  28. 28

    How to set value into HTML5 date field from Asp.Net MVC Razor?

  29. 29

    MVC 5 Html helper CheckBoxFor cannot be selected in Razor partial

HotTag

Archive