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'
You cant cast List<string>
to SelectList
, you need to initialise a new SelectList
eg
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 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.