JQuery在MVC 5中可以很好地处理整数,但不能处理字符串

JQuery is working fine with integer but not with string in MVC 5

本文关键字:处理 整数 字符串 但不能 很好 MVC JQuery      更新时间:2023-09-26

Jquery是工作完美的整数var,但得到错误的字符串。我的代码如下:

@section Scripts {
        <script type="text/javascript">
            $(function () {
                $.getJSON('/Service/ListServiceType', function (result) {
                    var ddl = $('#ServiceType');
                    var type =  @Model.Type;
                    ddl.empty();
                    $(result).each(function () {
                        $(document.createElement('option'))
                            .attr('value', this.Type)
                            .text(this.Type)
                            .appendTo(ddl);
                    });
                    $("#ServiceType").find("option").each(function () {
                        alert($(this).val())
                        if ($(this).val() == type) {
                            $(this).prop("selected", "selected");
                        }
                    });
                });
                $.getJSON('/Category/ListCategory', function (result) {
                    var ddl = $('#CategoryType');
                    var id = @Model.CategoryId;
                    ddl.empty();
                    $(result).each(function () {
                        $(document.createElement('option'))
                            .attr('value', this.Id)
                            .text(this.Name)
                            .appendTo(ddl);
                    });
                    $("#CategoryType").find("option").each(function () {
                        if ($(this).val() == id) {
                            $(this).prop("selected", "selected");
                        }
                    });
                });
            });
        </script>
    }

下面的部分(/Category/ListCategory)工作得很好,但上面的部分不行。由于我是JQuery和Javascript的新手,所以一点帮助将是值得赞赏的。

字符串类型应该用引号括起来。

改变这

var type =  @Model.Type; 

var type =  '@Model.Type';