【问题标题】:using IEnumerable value for autocomplete results are always null使用 IEnumerable 值进行自动完成结果始终为空
【发布时间】:2017-05-10 08:53:19
【问题描述】:

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

传递给控制器​​的值总是null

我认为问题出在这里

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

我需要获得 .Name 属性。我不想改变我的虚拟机。

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>

【问题讨论】:

  • @M.kazemAkhgary 是的,这是真的,但我的 null 值在 public JsonResult GetGenresName(string term)//Nulll 问题就在这里:@Html.TextBoxFor(m=>m.Genres 它应该得到名称值

标签: c# jquery asp.net-mvc autocomplete


【解决方案1】:

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

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

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

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

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 属性绑定:

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

现在控制器动作参数名称将是Term

public JsonResult GetGenresName(string Term)
{
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2019-05-31
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多