传递列表以查看以绑定自动完成搜索框

时间:2019-04-30 07:54:15

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

问题是,当页面加载时,我想绑定自动搜索,这样做,我不知道我应该使用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);
}

在上面的代码中,我不知道是否应该使用actionResultjsonResult来实现我想要的功能,我应该通过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.
                               })
                            )   

但是要绑定,我应该如何获取数据?

1 个答案:

答案 0 :(得分:1)

  

Ajax

[HttpPost]     公共JsonResult GetAutocomplete(字符串前缀)     {         var CarList =(来自DB.AccessinfoCars中的d                      选择新的{                          城镇= d。城市名称,                          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.
    )