1

this is my Model

public class StatModel
    {
            public IEnumerable<int> SelectedItemsRegion { set; get; }

            public List<string> SelectedStats { set; get; } // here I want to put selected Stats (value)

            public List<string> StatsList { set;  get; } //List of Values (stats) to select from it .

        public StatModel() 
            {
                StatsList = new List<string> {"agR", "demandeR" };
           }        

    }

This is my view :

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>StatModel</legend>
        @Html.DropDownList("REGIONID", String.Empty)

        @Html.DropDownListFor(model=> model.SelectedStats, Model.StatsList as SelectList)
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

I got an Error At @Html.DropDownListFor(model=> model.SelectedStats, Model.StatsList as SelectList)

CS0039: cannot convert 'System.Collections.Generic.List' to 'System.Web.Mvc.SelectList'

0

1 Answer 1

1

You cant cast List<string> to SelectList, you need to initialise a new SelectList e.g.

public SelectList SelectedStatsList { get; private set; } 

public StatModel() 
{
  StatsList = new List<string> {"agR", "demandeR" };
  SelectedStatsList = new SelectList(StatsList);
}

then in the view

@Html.DropDownListFor(model=> model.SelectedStats, Model.SelectedStatsList )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.