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

问题描述 投票:0回答:1

问题是,当页面加载时我希望自动搜索被绑定,为了做到这一点,我不知道我应该使用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.
                               })
                            )   

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

c# ajax asp.net-mvc kendo-ui
1个回答
1
投票

阿贾克斯

[HttpPost] public JsonResult GetAutocomplete(string prefix){var CarList =(from d in DB.AccessinfoCars select new {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.
    )
© www.soinside.com 2019 - 2024. All rights reserved.