对自动完成结果使用IEnumerable值始终为null [英] using IEnumerable value for autocomplete results are always null

查看:36
本文介绍了对自动完成结果使用IEnumerable值始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有以下字段,我试图在自动完成中使用它,如果该值不存在,请向表中添加新值.

I have the following fields in my View Model and I'm trying to use it in a autocomplete and in case the value does not exist add new value to table.

传递给控制器​​的值始终是null.

How ever the value passed to controller is always null.

我认为问题出在这里

"@ Html.TextBoxFor( m => m.Genres ,新{@class =" form-control,@ id =" term})"

"@Html.TextBoxFor(m=>m.Genres,new { @class = "form-control" ,@id= "term" }) "

我需要获取 .Name 属性.我不想更改我的VM.

I need to get the .Name property . I prefer not to change my VM.

public class LectureFormViewModel
{
        public int Id { get; set; }
       //Values 
        public byte Genre { get; set; }
        public IEnumerable<Genre> Genres { get; set; }
}

public JsonResult GetGenresName(string term)
{
    var genre = _context.Genres
        .Where(gn => term == null ||
         gn.Name.ToLower().Contains(term.ToLower())).Select(x => new
         { id = x.Id, value = x.Name }).Distinct().ToList();
    return Json(genre, JsonRequestBehavior.AllowGet);
}



@using (Html.BeginForm("Create", "VoosUp", FormMethod.Post, new {enctype = "multipart/form-data", id = "RestoForm"}))
{
    //values
    <div class="form-group">
        @Html.LabelFor(model => Model.Genre, new {@class = "control-label"})
        @Html.TextBoxFor(m => m.Genres, new {@class = "form-control", @id = "term"})
        @Html.ValidationMessageFor(m => m.Genres)
    </div>
    <input type="button" class="btn btn-primary btn-lg pull-right" onclick="GetLocation()" value="Finish"/>
}

@section scripts
{
    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $("#term").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '@Url.Action("GetGenresName", "VoosUp")',

                    data: "{'GetGenresName': '" + request.term + "' }",

                    dataType: 'json',
                    type: "POST",

                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return data; },
                    success: function(data) {
                        console.log(term),
                            console.debug();
                        response($.map(data,
                            function(item) {
                                return {
                                    label: item.value,
                                    value: item.value,
                                    id: item.id
                                }
                            }));
                    }
                });

            },
            minLength: 2
        });
    </script>

推荐答案

问题是您正在使用IEnumerable<Genre> type属性绑定文本框,并且您的控制器操作需要使用string类型的参数,因此您应该使用用于将文本框值映射到控制器操作的字符串类型属性,另一件事是,文本框名称将在发布时用于模型绑定,并且您将为文本框生成html,例如:

The problem is that you are binding the textbox with IEnumerable<Genre> type property and your controller action expects a parameter of type string, you should be using a string type property for mapping the textbox value to controller action, another thing is that the textbox name will be used for model binding at post, and you will have html generated for textbox like:

<input type="text" name="Genres" ................... />

但是在控制器操作中,您是参数名称term,该名称也将不起作用,因为该值将发布在名为Genres的参数中.

But in controller action you are parameter name term that would also not work, as the value will be posted in a parameter named Genres.

解决方案是在视图模型中为文本框绑定添加另一个属性,例如:

The solution is to add another property in your view model for textbox binding like:

public class LectureFormViewModel
{
        public int Id { get; set; }
        public string Term {get;set;}
        public byte Genre { get; set; }
        public IEnumerable<Genre> Genres { get; set; }
}

现在,在您的视图中,将TextBox与Term属性绑定:

Now in your view, bind the TextBox with the Term property :

@Html.TextBoxFor(m => m.Term, new {@class = "form-control"})

,并且在控制器操作中,参数名称现在为Term:

and in controller action parameter name will be Term now:

public JsonResult GetGenresName(string Term)
{
}

这篇关于对自动完成结果使用IEnumerable值始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆