Voici ce que je suis en train de faire:ModelBinder avec dropdownlist dans asp.net mvc
J'ai une entité Task avec une propriété TaskName et une propriété TaskPriority .
Maintenant, dans le code html j'ai:
<td><%=Html.TextBox("Task.TaskName") %></td>
<td><%=Html.DropDownList("Task.TaskPriority",new SelectList(ViewData.Model.TaskPriorities,"ID","PriorityName")) %></td>
L'action du contrôleur est comme ceci:
public ActionResult Create(Task task){
//task.TaskName has the correct value
//task.TaskPriority is null - how should the html look so this would work ?
}
EDIT Dans l'exemple ci-dessous (à partir Schotime):
public class Task
{
public int name { get; set; }
public string value { get; set; } // what if the type of the property were Dropdown ?
// in the example I gave the Task has a property of type: TaskPriority.
}
Voulez-vous dire que TaskPriority est une classe ?? Si c'est le cas, vous devez spécifier Task.TaskPriority.PropertyName pour le nom de la liste déroulante. – Schotime
Cela fonctionne en effet :). S'il vous plaît modifier votre réponse afin que je puisse l'accepter. – sirrocco