2017-06-02 14 views
2

管理控制器jQuery用户界面自动完成在局部视图不工作

public ViewResult Products() 

public PartialViewResult AddProduct() 

public JsonResult AutoComplete(string prefix) 

查看

我加载在主视图模式弹出的局部视图(AddProduct.cshtml)。

在部分视图中我有一个表单,我试图在输入字段添加jQuery UI自动完成,但它不工作。

$(function() { 
     $("#txtProductName").autocomplete({ 
      source: function(request, response) { 
       $.ajax({ 
        url: '/Admin/AutoComplete/', 
        data: "{ 'prefix': '" + request.term + "'}", 
        dataType: "json", 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        success: function(data) { 
         response($.map(data, function(item) { 
          return item; 
         })) 
        }, 
        error: function(response) { 
         alert(response.responseText); 
        }, 
        failure: function(response) { 
         alert(response.responseText); 
        } 
       }); 
      }, 
      minLength: 1 
     }); 
    }); 

我已经添加在局部视图页面本身这jQuery代码,还当我试图使用太不工作

+0

控制台中的任何错误? “自动完成不是功能”还是没有错误? – adiga

+0

没有错误我已经写了console.log,但它没有在控制台中打印,所以自动完成不会被触发 –

+0

现在我在控制台中出现此错误“产品:1未捕获TypeError:autocomplete不是HTMLInputElement.onclick(产品:1 )“ –

回答

2

最后我能解决这个问题,我自己的AJAX保存表单数据。

这里是我的控制器代码

[HttpGet] 
public ViewResult Products() - Listing all the products (used for main view) 

[HttpGet] 
public PartialViewResult AddProduct() - Add new product form (used for partial view) 

[HttpPost] 
public JsonResult AutoComplete(string prefix) - Fetching data from database for autocomplete 

查看

Product.cshtml - Html for listing all the products (Main View) 

AddProduct.cshtml - Html for add new product form (Partial View) 

所以我添加了所有的jQuery refrence在_Layout.cshtml(共享视图)文件,并在局部视图我在页面底部添加了以下jQuery代码

$("#txtProductName").autocomplete({ 
    rest of the logic and ajax hit goes here... 
}); - used to show autocomplete suggestion when user types 

$("#AddNewProduct").on("submit", "#AddNewProductForm", function (e) { 
    $.ajax({ 
     rest of the logic goes here... 
    }); 
}); - used to submit the form data 
相关问题