在淘汰赛中映射复杂的MVC ViewModel

时间:2012-08-06 15:28:35

标签: model-view-controller knockout.js knockout-mapping-plugin

我有一个MVC视图,其中包含observables和observablearrays。映射的正确方法是什么?

我的MVC ViewModel看起来与此类似:

public class CategorysEditViewModel
{
    public Category Categorys { get; set; }
    public IEnumerable<Expert> candidates { get; set; }
    public IEnumerable<Expert> experts { get; set; }
}


public class Category
{
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Policy { get; set; }
}

public class Expert
{
    public int ExpertID { get; set; }
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public string ExpertGUID { get; set; }
}

1 个答案:

答案 0 :(得分:2)

假设cshtml文件强类型为CategorysEditViewModel,您可以将视图模型对象序列化为JSON,然后使用ko.mapping工具将其映射到knockout viewmodel。

查看此帖子:Best practice on passing Mvc Model to KnockoutJS