Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo dropdownlist produces TypeError: n.slice is not a function

Do I need to define the schema? If so, what should that look like? My searches for this seem to only turn up js solutions, I'm looking for the syntax to define it in the editortemplate.

Shared/editortemplate:

@(
Html.Kendo().DropDownList()
.Name("SearchFunction")
.DataTextField("SearchFunctionDesc")
.DataValueField("SearchFunctionCode")
.DataSource(source =>
    {        
        source.Read(read => { 
            read.Action("GetSearchFunctions", "User");
        });
    })
    .OptionLabel("--Select a Search Function--")
    .AutoBind(false)
)

In the controller:

    public JsonResult GetSearchFunctions([DataSourceRequest] DataSourceRequest request)
    {
        var searchFuncs = AdminService.GetSearchFunctions();
        DataSourceResult result = searchFuncs.ToDataSourceResult(request);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

And then my Dapper db query:

            var result = new List<SearchFunction>();
            using (var conn = new OracleConnection(DatabaseConnectionString))
            {
                conn.Open();
                string query = "select FUNCTION_ID, SEARCH_FUNCTION_CD, " +        
                        "SEARCH_FUNCTION_DESC, IS_ACTIVE " +
                         "from TBL_SEARCH_FUNCTIONS "; 
                result = conn.Query(query)
                    .Select(s => new SearchFunction
                    {
                        FunctionId = (int)s.FUNCTION_ID,
                        SearchFunctionCode = s.SEARCH_FUNCTION_CD,
                        SearchFunctionDesc = s.SEARCH_FUNCTION_DESC,
                        Active = s.IS_ACTIVE
                    }).ToList<SearchFunction>();
                conn.Close();
                return result;
            }
like image 868
littleGreenDude Avatar asked Jun 05 '14 17:06

littleGreenDude


3 Answers

Rewrite your controller method like this:

public JsonResult GetSearchFunctions()
{
    var searchFuncs = cmsViewAdminService.GetSearchFunctions();
    return Json(searchFuncs, JsonRequestBehavior.AllowGet);
}

That should simplify that method as you don't need the DataSourceRequest (as @CSharper mentioned in the comment). Kendo DropDownLists, unlike the grids, don't require the DataSourceRequest class. This way, you can call the same JsonResult from a jQuery Ajax method if you needed to do so.

like image 97
piercove Avatar answered Nov 17 '22 07:11

piercove


You need to return a pure collection from json something that looks like that

{[
    {"Id":2,"Name":"some"},
    {"Id":3,"Name":"som2"}
]}
like image 31
COLD TOLD Avatar answered Nov 17 '22 07:11

COLD TOLD


If you use ModelView and lambda then you might also try to the code below:

Assume you have a city dropdownlist retrieving data from reference table (populated to CityViewModel):

public JsonResult GetCities()
{
    var dataContext = new EFDbContext();

    var cities = dataContext.Cities.Select(c => new CityViewModel
    {
        ID = c.ID,
        Name = c.Name
    });
    return Json(cities, JsonRequestBehavior.AllowGet);
}

Regards.

like image 1
Murat Yıldız Avatar answered Nov 17 '22 09:11

Murat Yıldız