【问题标题】:JsRender does not make the transformation for a for loopJsRender 不会为 for 循环进行转换
【发布时间】:2012-09-19 09:10:33
【问题描述】:

代码按照 JsFiddle 工作; http://jsfiddle.net/arame3333/PTWnq/6/

但是在我的 MVC 项目中,它在页面上呈现为;

{{ for BusinessUnitName }} •{"BusinessUnitName":["Design and Build (Technical)","Architectual Design","ICT","Mechanical & Electrical ","Safety Management","Pre-Construction &投标准备"]} {{/for }}

为什么会这样?

相关代码是;

<ul id="departmentList"><%: Html.DisplayFor(model => model.AdditionalDepartmentList) %></ul>



<script id="DepartmentTemplate" type="text/x-jquery-tmpl">
            {{ for BusinessUnitName }}
                <li>{{:#data}}</li>
            {{/for }}
    </script>



function RefreshDepartments(o) {
        alert("Here are the results");
        var url = '<%: Url.Action("GetAdditionalDepartments", "DataService")%>';
        dataService.getAdditionalDepartments(
            o.EmployeeId,
            RenderDepartments,
            url
        );
    }

    RenderDepartments = function (data) {
        $('#departmentList').empty();
        $('#departmentList').html($('#DepartmentTemplate').render(data));
    };

数据来自这里的控制器;

public ActionResult GetAdditionalDepartments(int employeeId)
        {
            var list = AdditionalDepartment.GetBusinessNamesByEmployeeId(employeeId);
            var jss = new JavaScriptSerializer();
            var json = jss.Serialize(new JsonArrayFormat(list.ToList()));
            return new JsonResult
                       {
                           Data = json,
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }

已编辑的标记 - 我无法全部显示;

    <form method="post" action="/Employee/Edit"><input name="__RequestVerificationToken" value="IM9yFknHf1HKgZbd+UYsyMs9wQKL9dPK8zcRfi5uXTRQl7JvCq/Gr6cVq1wnUnao5Y2IToCDtEb5Vz/KnnxXSEDartM7fE9mQ7Cm7ynsFVE6ST2hhcygJX1ALAP67AXVlBBSf5ozczCctqb8ok+Blw==" type="hidden">

        <input id="Employee_EmployeeId" name="Employee.EmployeeId" value="258" type="hidden">
        <input id="Employee_ApproverEmployeeId" name="Employee.ApproverEmployeeId" value="340" type="hidden">
        <input id="CurrentCostRate_EmployeeId" name="CurrentCostRate.EmployeeId" value="258" type="hidden">
        <input id="CurrentCostRate_CostRateId" name="CurrentCostRate.CostRateId" value="303" type="hidden">
        <input id="Employee_UserName" name="Employee.UserName" value="aaronchampion" type="hidden">
        <input id="Employee_BusinessUnit_BusinessUnitName" name="Employee.BusinessUnit.BusinessUnitName" value="Construction" type="hidden">
        <fieldset>
            <legend>Edit Employee</legend>

            <table id="editTable" class="groupBorder">


                      <tr>
                          <td style="text-align: right;">Additional Departments</td>
                          <td colSpan="2">
                              <span style="display: inline-block;">

<script type="text/javascript">
        $(function () {
            $("#DialogAdditionalDepartmentsAdditionalDepartment").dialog({
                show: "fade",               
                width: 620,
                height: 850,
                title: 'Additional Departments/Divisions for Aaron Champion',
                modal: true,
                resizable: true,
                autoOpen: false,
                close: function () { $("#DialogAdditionalDepartmentsAdditionalDepartment").html(""); },
                buttons: {
                    "": function () { $(this).dialog('close'); }, 
                    "Close": function () { $("#DialogAdditionalDepartmentsAdditionalDepartment form").submit(); } 
                }
            }); 
        });

        var lockpopupDialogAdditionalDepartmentsAdditionalDepartment = null;
        function callDialogAdditionalDepartmentsAdditionalDepartment(employeeId) { 
            if(lockpopupDialogAdditionalDepartmentsAdditionalDepartment != null) return;
            lockpopupDialogAdditionalDepartmentsAdditionalDepartment = true;
            $.get('/AdditionalDepartment/AdditionalDepartments',
             { employeeId : employeeId }, 
            updateDialogAdditionalDepartmentsAdditionalDepartment
            );
        }

        function OnSuccessDialogAdditionalDepartmentsAdditionalDepartment(result) {
            if (result == 'ok') {
                $("#DialogAdditionalDepartmentsAdditionalDepartment").dialog('close');

            }            

            else
            if(typeof(result) == 'object'){
            $("#DialogAdditionalDepartmentsAdditionalDepartment").dialog('close');
             RefreshDepartments(result);
            }

            else {
                updateDialogAdditionalDepartmentsAdditionalDepartment(result);
            }
        }        

        function updateDialogAdditionalDepartmentsAdditionalDepartment(data) {
            lockpopupDialogAdditionalDepartmentsAdditionalDepartment = null;
            $("#DialogAdditionalDepartmentsAdditionalDepartment").html(data);
            $("#DialogAdditionalDepartmentsAdditionalDepartment form").ajaxForm({


                beforeSubmit: function () { return $("#DialogAdditionalDepartmentsAdditionalDepartment form").validate().valid(); },

                success: OnSuccessDialogAdditionalDepartmentsAdditionalDepartment
            }); //ajaxForm
            $("#DialogAdditionalDepartmentsAdditionalDepartment").dialog('open');
            $("#DialogAdditionalDepartmentsAdditionalDepartment form input:visible:first").focus();
        }
</script>


                                <a href="javascript:callDialogAdditionalDepartmentsAdditionalDepartment(258)">Add/Edit additional departments/divisions</a></span>
                              <ul id="departmentList">

<li>Architectual Design</li>

<li>ICT</li>

<li>Mechanical &amp; Electrical </li>

<li>Safety Management</li>

<li>Estimating</li>

<li>HR and Training Development</li></ul>
                          </td>
                      </tr>


            </tbody></table>
        </fieldset>

    </form>

    <div>
        <a href="/Employee/List">Back to List</a>
    </div>

    <script id="DepartmentTemplate" type="text/x-jquery-tmpl">
        {{ for BusinessUnitName }}
            <li>{{:#data}}</li>
        {{/for }}
    </script>

    <script type="text/javascript">
        function RefreshDepartments(o) {
            alert("Here are the results");
            var url = '/DataService/GetAdditionalDepartments';
            dataService.getAdditionalDepartments(
                o.EmployeeId,
                RenderDepartments,
                url
            );
        }

        RenderDepartments = function (data) {
            $('#departmentList').empty();
            var data1 = $.parseJSON(data);
            $('#departmentList').html($('#DepartmentTemplate').render(data1));
        };


    </script>



        </div>
    </div>

【问题讨论】:

    标签: asp.net-mvc jsrender


    【解决方案1】:
    <script id="DepartmentTemplate" type="text/x-jquery-tmpl">
        {{ for BusinessUnitName }}
             <li>{{:#data}}</li>
        {{/for }}
    </script>
    

    问题出现在 3 个空格中,很难找到问题,因为 jsfiddle.net 示例运行良好。在所有尝试检测问题之后,我向 arame3333 询问了 htmp-markup,然后我尝试了一下,发现模板中的问题,更正一个:

    <script id="DepartmentTemplate" type="text/x-jquery-tmpl">
        {{for BusinessUnitName}}
             <li>{{:#data}}</li>
        {{/for}}
    </script>
    

    【讨论】:

    • 我已在问题中填写了您要求的额外信息。
    • @arame3333 很难理解的问题,不调试,使用两个断点(一个在动作return new JsonResult)和另一个在javascript中RenderDepartments 并比较结果。
    • 数据看起来像是来自您建议的断点; "{\"BusinessUnitName\":[\"Design and Build (Technical)\",\"Architectual Design\",\"ICT\",\"Mechanical & Electrical \",\"质量管理\"]}"
    • @arame3333 试试parseJSON 并检查javascript函数(我使用firebug)。
    • @arame3333 抱歉,为了混淆,我将其删除(我正在考虑更新)。这是演示:jsfiddle.net/PTWnq/7
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2012-10-13
    • 2015-12-03
    • 1970-01-01
    • 2021-03-12
    • 2018-02-22
    相关资源
    最近更新 更多