我正在使用 Asp MVC 3 应用程序。
我有一个枚举:
public enum EmployeesOptions
{
John = 1,
Peter = 2,
Andrew = 3
}
和一MyViewModel
堂课
public class MyViewModel
{
public MyViewModel()
{
Employees = new List<EmployeesOptions>()
{
EmployeesOptions.John,
EmployeesOptions.Peter,
EmployeesOptions.Andrew
};
}
public IEnumerable<EmployeesOptions> Employees { get; set; }
}
我的控制器:
public ActionResult Index()
{
var vm = new MyViewModel();
return View(vm);
}
在我的索引视图中:
@model MyViewModel
<script type="text/javascript">
var jsonString = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model))';
var data = ko.mapping.fromJSON(jsonString);
omega.franchiseInfo = ko.mapping.fromJS(data);
</script>
我来自服务器的序列化数据如下所示:
Emplyees:[1,2,3]
我想变成这样:
Emplyees:["John","Peter","Andrew"]
我错过了什么?
更新:
var jsonString = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model, Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.Converters.StringEnumConverter()))';
这做的工作!