【问题标题】:Using jsViews/jsRender with a property retuned as dictionary entries将 jsViews/jsRender 与作为字典条目重新调整的属性一起使用
【发布时间】:2015-03-11 00:50:14
【问题描述】:

我想知道为什么似乎无法正确记录下来。我在 MVC 中有一个 JSONResult 返回“作业”对象的集合。 “作业”对象的属性之一是字典。我似乎无法弄清楚如何将其添加到“作业”实体模板中,以便可以显示内部模板的所有值。当前的对象设计将是一个作业,每个作业都有一个作业阶段字典。以下代码是我目前使用的:

Job.cs

  public string Id { get; set; }
    public string Name { get; set; }
    public string CustomerName { get; set; }
    public string Phase {get;set;}
    public string PhaseText { get; set; }
    public Dictionary<String,String> Codes { get; set; }

客户端脚本:

<script id="jobLevelTmpl" type="text/x-jsrender">
        <tr class="row-header">
            <td class="col-lg-1">{{:Id}}</td>
            <td class="col-lg-4">{{:Name}}</td>
            <td class="col-lg-1" title="Status code: '{{:Phase}}'">{{:PhaseText}}</td>
            <td class="col-lg-1"><a jobid="{{:Id}}">View Job Details</a></td>
            <td class="col-lg-1">
                <div class="dropdown">
                    <button class="btn btn-warning dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">
                        Phase
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
                        {{for Codes}}
                           <!-- This is where I do not know what to call or how to call the key or value of the dictionary items -->                            
                           <li role="presentation" class="dropdown-header">{{>code}}</li>
                        {{/for}}
                   </ul>              
               </div>
            </td>
            <td class="col-lg-1"><a href="javascript:function(){return false;}" jobid="{{:Id}}" class="btn btn-info expand-job">Update</a></td>
        </tr>
        <tr>
            <td class="col-lg-12 details-info" style="display:none;">
                <a href='#' onclick='parent.$.colorbox.close(); return false;' style="float:right;">Close</a>

            </td>
        </tr>      
    </script>

【问题讨论】:

    标签: javascript jquery jsviews


    【解决方案1】:

    如果Codes 的客户端 JSON 对应于带有字符串值的纯 JavaScript 对象,那么您可以使用 {{props}} 遍历属性 - 例如:

    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
        {{props Codes}}
            <li role="presentation" class="dropdown-header">Key: {{>key}} Value: {{>prop}}</li>
        {{/props}}
    </ul> 
    

    http://www.jsviews.com/#propstag

    【讨论】:

    • 太棒了,很抱歉耽搁了。我在被问到问题后不久就昏倒了。我尝试了这个,除了我忘记了更大的符号,但这开辟了更多我现在可以用它做的事情。一如既往地再次感谢!继续努力。
    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多