【问题标题】:Map a complex MVC ViewModel in knockout在淘汰赛中映射复杂的 MVC ViewModel
【发布时间】:2012-08-07 02:19:33
【问题描述】:

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

我的 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; }
}

【问题讨论】:

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


    【解决方案1】:

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

    查看这篇文章:Best practice on passing Mvc Model to KnockoutJS

    【讨论】:

    • 我已按照您的建议使用 ko.mapping 工具。但是现在我决定我不知道如何在 HTML 代码中引用这些不同的 observables。这是一个显示我当前情况的 jsFiddle (jsfiddle.net/gilley81/Q4pRL) 链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2012-12-05
    • 2015-03-30
    • 2017-07-07
    • 2014-03-26
    相关资源
    最近更新 更多