La difficulté à obtenir DropDownListFor travailler sur EditorFor dans MVC3
-
27-10-2019 - |
Question
J'ai un problème assez simple qui a une solution, je ne suis pas en mesure de trouver.
Étant donné le modèle suivant:
public class InfoViewModel
{
public SelectList States { get; set; }
[DisplayName("State")]
public string State { get; set; }
}
Les travaux suivants bien à mon avis:
@Html.DropDownListFor(m => m.State, Model.States)
Cependant, si je tente de tirer cela dans un modèle d'éditeur (nommé « SelectList »), tels que:
@model System.String
@Html.DropDownListFor(m => m, ViewData["selectList"])
Et puis utiliser EditorFor
pour construire la liste déroulante:
@Html.EditorFor(m => m.State, "SelectList", new { selectList = Model.States })
Il échoue avec l'erreur suivante:
'System.Web.Mvc.HtmlHelper<string>' does not contain a definition for
'DropDownListFor' and the best extension method overload
'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>
(System.Web.Mvc.HtmlHelper<TModel>,
System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>,
System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)'
has some invalid arguments
Je vais avoir du mal à comprendre la différence entre ces deux. J'ai essayé plusieurs solutions de contournement pour résoudre les problèmes, et soit obtenir la même erreur, ou autre chose.
Merci à l'avance.
La solution
There is no such overload:
@Html.DropDownListFor(m => m, ViewData["selectList"])
The second parameter of the DropDownListFor
helper musty be a SelectList
. So:
@Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow