我正在使用Asp MVC 3應用程序。序列化枚舉爲常量JsonNet
我有一個枚舉:
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()))';
該做的工作!
幫了我很多!感謝分享解決方案... –