I have a DropDownList control inside of my model, TaskDetailsModel. I’ve declared my DropDownList as:
<%= Html.DropDownListFor(model => model.TaskType, new SelectList(Model.TaskTypeDisplayNames, Model.TaskTypes))%>
My model’s corresponding class contains the following property declarations:
public class TaskDetailsModel
{
[DisplayName("Task Type")]
public TaskType TaskType { get; set; }
public List<TaskType> TaskTypes = new List<TaskType> { TaskType.Decommission, TaskType.Install, TaskType.Modify, TaskType.Move, TaskType.Other, TaskType.Rename };
public List<string> TaskTypeDisplayNames = new List<string> { "Decommission", "Install", "Modify", "Move", "Other", "Rename" };
}
When everything loads and displays to the user — the correct TaskType selection has been made. That is, I have no problem loading with the non-default select option. As such, I believe that at least part of this code is working properly.
However, any changes made to the select are lost. When I attempt to post my form back to the server — I see that my selected option never changes. I was wondering what I am doing incorrectly to incur such behavior.
Update:
public TaskDetailsModel(Task task)
{
TaskType = task.TaskType;
}
//Client-side code to retrieve form values:
var getDialogData = function () {
var dialogData = {};
$(workflowDialogContent).find('input,p,select').each(function () {
if ($(this).is('input')) {
dialogData[this.id] = $.trim($(this).val());
}
else if ($(this).is('select')) {
//At this point in time the selected option does not match what I see on the screen. The user has modified the selected value. I do not see it reflected here.
console.log("Inside here for:", this);
dialogData[this.id] = $.trim($(this).find('option:selected').val());
}
else {
dialogData[this.id] = $.trim($(this).text());
}
});
return JSON.stringify(dialogData);
};
The issue is actually with the fact that I am renaming my enum values in an apparently improper fashion. The only values which get lost when passing back to the server are selections which have spaces in them. (e.g. changing to ‘Decommission’ is fine, but changing to ‘Waiting For Customer’ fails to translate properly).