Home MVC5 Razor html.dropdownlistfor set selected when value is in array
Reply: 0

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

user11974
1#
user11974 Published in September 20, 2018, 12:45 pm

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?

share|improve this question
  • Its an unfortunate limitation of using DropDownListFor() in a loop. You need to generate a new MutliSelectList in each iteration (setting the selectedValues property in the constructor), or (better), use an EditorTemplate for typeof AggregationLevelConfiguration and pass the SelectList to the template as AdditionalViewData – Stephen Muecke May 24 '16 at 8:14
  • Thanks. I have understood that I have to use things but I don't know how to use them. – VansFannel May 24 '16 at 8:25
  • I'm trying to find an answer I gave about 6 months ago with the code for both options but not having any luck. If I cannot find it, I'll add an answer in an hour or so - but the following should work - @Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.HelperCodeTypeItems, "Value", "Text", Model.Configurations[0].HelperCodeType, new { ... }) – Stephen Muecke May 24 '16 at 8:30
  • I'm working on a solution using jQuery to select the option. – VansFannel May 24 '16 at 8:32
  • Yes, that works. Thanks. – VansFannel May 24 '16 at 8:35

2 Answers 2

active oldest votes
up vote 15 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.505224 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO