【问题标题】:pass a list to view to bind an autocomplete search box将列表传递给视图以绑定自动完成搜索框
【发布时间】:2019-04-30 07:54:14
【问题描述】:

问题是,当页面加载时,我希望绑定自动搜索,为了做到这一点,我不知道我应该使用 jsonresult 还是只使用操作结果?要指出这里的问题是我已在我的控制器中完成:

[HttpPost]
public JsonResult IndexSearch () {
    //List of cars            
    var CarList = (from d in DB.AccessinfoCars select new {

        Town = d.City_name,
            CarName = d.Car_name
    }).ToList ();

    return Json (CarList, JsonRequestBehavior.AllowGet);
}

在上面的代码中,不知道是应该使用actionResult还是jsonResult来实现我想要的,我应该通过viewBag还是Ajax调用?

在我看来,我只想绑定以下自动完成功能:

    @(Html.Kendo().AutoComplete()
                              .Name("CarName") //The name of the AutoComplete is mandatory. It specifies the "id" attribute of the widget.
                              .DataTextField("input") //Specify which property of the Product to be used by the AutoComplete.
                              .DataSource(source =>
                               {
                                  source.Read(read =>
                                  {
                                      read.Action("IndexSearch", "Overview"); //Set the Action and Controller names.
                                  })
                                  .ServerFiltering(true); //If true, the DataSource will not filter the data on the client.
                               })
                            )   

但是要绑定那个我应该如何获取数据呢?

【问题讨论】:

  • 如果您想要自动完成,则需要从浏览器通过 AJAX 调用它,否则您将无法获得实时结果。我不知道 kendoAutocomplete 但我猜它会自动生成一个,如果你告诉它要访问的 URL 和要传递的参数的名称。是的,返回 JSON 可能是有意义的。查看 kendo 控件的文档以了解如何配置它,以及它期望从服务器返回的数据格式。
  • @ADyson 但通常如果我想在通过 Ajax 加载页面之前调用控件,我需要发送参数对吗?如果没有参数发送我应该怎么做?例如我想搜索某个参数应该是名称或其他东西,如果我需要一个没有参数的列表我该怎么办?据我所知使用 ajax 我需要一个参数
  • 我不确定我是否理解问题所在。 AJAX 调用在页面/视图加载之后运行。它们是单独的 HTTP 请求。您的控制器有一个请求加载视图。然后当用户输入自动完成时,它会向服务器生成第二个(AJAX)请求,并传递用户刚刚输入的值。第二个请求应该转到与加载视图的不同的操作方法。这个单独的操作方法应该是接受参数并返回 JSON 的方法。如果用户键入更多,它会生成另一个 AJAX 请求,依此类推...
  • P.S.如果您希望您的操作方法在未发送任何参数时返回所有值,那么您可以将参数设为可选。

标签: c# ajax asp.net-mvc kendo-ui


【解决方案1】:

阿贾克斯

[HttpPost] 公共 JsonResult GetAutocomplete(字符串前缀) { var CarList=(来自 DB.AccessinfoCars 中的 d 选择新的{ Town=d.City_name, CarName=d.Car_name }).ToList();

    return Json(CarList,JsonRequestBehavior.AllowGet);
}

剃须刀

 @(Html.Kendo().AutoComplete()
      .Name("productAutoComplete") //The name of the AutoComplete is mandatory. It specifies the "id" attribute of the widget.
      .DataTextField("input") //Specify which property of the Product to be used by the AutoComplete.
      .DataSource(source =>
       {
          source.Read(read =>
          {
               read.Action("GetAutocomplete", "yourControler"); //Set the Action and Controller names.
          })
          .ServerFiltering(true); //If true, the DataSource will not filter the data on the client.
       })
    )

型号

public ActionResult Index()
{
    YourModel model = new YourModel();

    return View(model );
}

@model your modal

 @(Html.Kendo().AutoComplete()
        .Name("yourName") //The name of the AutoComplete is mandatory. It specifies the "id" attribute of the widget.
        .DataTextField("nameYourControl") //Specify which property of the Product to be used by the AutoComplete.
        .BindTo(Model) //Pass the list of Products to the AutoComplete.
        .Filter("contains") //Define the type of the filter, which AutoComplete will use.
    )

【讨论】:

  • 你也可以用 HTML 编写吗?因为我收到类似这样的错误 错误 29 'System.Web.Mvc.HtmlHelper' 不包含 'Kendo' 的定义并且没有扩展方法 'Kendo ' 可以找到接受“System.Web.Mvc.HtmlHelper”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
  • 我按照你的建议做了,当我点击指向我的视图的链接时,我得到“找不到资源”
  • 你能用模型代替ajax调用吗?
  • 什么意思?
  • 存在第二种使用方式。我在答案中添加了这种方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 2016-05-07
  • 1970-01-01
  • 2015-05-26
  • 2013-10-15
  • 1970-01-01
相关资源
最近更新 更多