我的模型 TaskDetailsModel 中有一个 DropDownList 控件。我已将我的 DropDownList 声明为:
<%= Html.DropDownListFor(model => model.TaskType, new SelectList(Model.TaskTypeDisplayNames, Model.TaskTypes))%>
我的模型对应的类包含以下属性声明:
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" };
}
当所有内容都加载并显示给用户时 - 已做出正确的 TaskType 选择。也就是说,我使用非默认选择选项加载没有问题。因此,我相信至少有一部分代码可以正常工作。
但是,对选择所做的任何更改都将丢失。当我尝试将表单发布回服务器时——我看到我选择的选项永远不会改变。我想知道我做错了什么会导致这种行为。
更新:
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);
};