jquery - Controller 中的参数在 asp.net mvc 中给出空值异常

标签 jquery asp.net ajax asp.net-mvc asp.net-core

我正在使用 jquery 和 ajax 制作一个带有下拉菜单的文本框,用于自动完成建议,但我通过调试发现的错误是,当调试点到达给定代码时,它成功移动到 return 语句,并且何时出现第二次在同一个语句上,它给出了一个异常(exception):“关键字为空”(关键字是用户在文本框中输入的字母表)

var result = (from a in objGameList where
a.GameName.ToLower().StartsWith(keyword.ToLower()) 
select new { a.GameName });

这是代码:

Index.cs

<script type="text/javascript">
    $(document).ready(function () {
        $("#GameName").autocomplete({
            source: function (request, response) {
                $.ajax({    
                    url: "/Home/Index",    
                    type: "POST",    
                    dataType: "json",    
                    data: { keyword: request.term },   
                    success: function (data) {   
                        response($.map(data, function (item) {    
                            return { label: item.GameName, value: item.GameName };    
                        }))    
                    },    
                    error: function () {    
                        alert('something went wrong !');    
                    }    
                })    
            },   
            messages: {   
                noResults: "", results: ""    
            }    
        });    
    })    
</script>
        
@using (Html.BeginForm())    
{    
    @Html.AntiForgeryToken()   
    <div class="form-horizontal">    
        <div class="form-group">    
            <div>       
                @Html.EditorFor(model => model.GameName, new { htmlAttributes = new { @class = "form-control" } })   
            </div>    
        </div>        
    </div>   
}
```

Controller.cs

[HttpPost]
    public JsonResult Index(string keyword)
    {
        //This can be replaced with database call.
    
        List<Games> objGameList = new List<Games>(){
            new Games {Id=1,GameName="Cricket" },
            new Games {Id=2,GameName="Football" },
            new Games {Id=3,GameName="Chesh" },
            new Games {Id=4,GameName="VallyBall" },
        };
    
        var result = (from a in objGameList
                        where a.GameName.ToLower().StartsWith(keyword.ToLower())
                        select new { a.GameName });
    
        return Json(result);
    }

提前致谢。

最佳答案

所以我从头开始做了一个例子,看看它是否行不通......它有效,所以请按照这个例子并尝试找出你的方法有什么不同。我将指出与您提供的代码片段相比我所做的每一个更改。

我将从新的空项目开始。

dotnet new mvc -o test

我将您的整个 POST 操作复制并粘贴到 HomeController.cs 中,修复导入并生成缺失的 Games 模型,将其放入文件放入 .\Models\Games.cs:

namespace test.Models
{
    public class Games // changed the default visibility to public
    {
        public int Id { get; set; }
        public string GameName { get; set; }
    }
}

我用您的 View 替换了 ./Views/Home/Index.cshtml 的内容(在文件顶部添加了以下行:@Model Games 让查看了解模型,以便 @Html.EditorFor() 能够生成表单控件 - 如果没有它,我会收到错误:CS1963 表达式树可能不包含动态操作):

所以:

@model Games
<script type="text/javascript">
...

我已将 jQuery 导入放入 .\Shared\_Layout.cshtml:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
...

并删除了对任何其他 js 文件的引用:

...
    </footer>
</body>
</html>

最后我编辑了 HomeController 的默认操作:

public IActionResult Index()
{
  return View(new Games()); // added new Games object to be injected as a model for the view.
}

在那个阶段我没有遇到你所描述的任何事情。虽然我收到错误:Uncaught TypeError: this.options.messages.results is not a function 但这是完全不同的东西,可以通过替换来解决

    messages: {
        noResults: "", results: ""
    }
});

这样:

    messages: {
        noResults: "", results : function(count) {
        return "";
      }
    }
});

解决方案结果没有错误和工作机制。

请尝试一下,它应该可以工作。我能想到的只是不同的库版本。

关于jquery - Controller 中的参数在 asp.net mvc 中给出空值异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243195/

相关文章:

javascript - 有没有可以在没有任何事件的情况下显示日历的插件

jquery - 使用 JQuery 填充 TinyMCE 文本区域(移动设备)

php - Typeahead 输入字段和查询通过 AJAX 传递给 PHP

javascript - jquery从mysql数据库实时搜索?

javascript - 变量未定义错误(即使console.log显示变量)

javascript - 即使删除只读后也无法使 TextArea 可编辑 : Html

javascript - 动态创建的元素上的事件绑定(bind)?

c# - 我对 String.EndsWith 的使用有什么问题?

c# - Microsoft asp c# RegEx 转义多个保留字符

asp.net - jQuery - Ajax - 当用户更改 DropDownList 中选定的索引时,如何将 ascx 加载到 DIV 中