This is a migrated thread and some comments may be shown as answers.

unable to bind data from remote

4 Answers 142 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Michele
Top achievements
Rank 2
Michele asked on 29 Jul 2013, 02:26 PM
Hello,
I'm trying to load some combobox values from the controller... but when I open the dropdown I got 2 items undefined (even if in the controller for test I return an empty collection)...what am I doing wrong?
Thanks

View :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<KendoUIMvcApplication1.TestObj>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    PROVA HEADER
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <%
       Html.Kendo().ComboBox()
           .Name("comboBox")
           .DataTextField("Description")
           .DataValueField("Value")
           .AutoBind(false)
           .Placeholder("Selezionare un oggetto")
           .DataSource(ds =>
               {
                   
                  // ds.Ajax()
                  // .ServeOperation(false)    
                   ds.Read(read =>
                       {
                           read.Action("GetCombo", "TestIDEA");
                       });
               })
               .Events(events =>
                   {
                       events.Change("Test");
                   })
               .Render();
        %>
   
    <script>
        function Test(e) {
            alert('changed');
        }
        
    </script>
</asp:Content>

Controller :

   public ActionResult GetCombo([DataSourceRequest] DataSourceRequest request)
        {

            List<TestObj> myList = new List<TestObj>();

            //myList.Add(new TestObj { Value = "1", Description = "A1" });
            //myList.Add(new TestObj { Value = "2", Description = "A2" });

            return Json(myList, "text/html", Encoding.UTF8, JsonRequestBehavior.AllowGet);
          
        }

Thanks

4 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 30 Jul 2013, 02:17 PM
Hi Paolo,


The reason for the issue is the content type that is specified for the JsonResult. Since the result is a collection of object, it is not "text/html". It should be left at the default "application/json".
E.g.
return Json(myList, JsonRequestBehavior.AllowGet);

I hope that this information was helpful for you. I wish you a great day!

 

Regards,
Dimiter Madjarov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Michele
Top achievements
Rank 2
answered on 30 Jul 2013, 02:19 PM
Dimiter I'm still beating my head up on data bindings..... on combo and on grid as well... is there a way to understand why my method is no invoked?

    <% Html.Kendo().Grid<IDEA.Web.DO.Administration.Utente>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(x => x.IDUtente);
        columns.Bound(x => x.Nominativo);
        columns.Bound(x => x.Societa);
        columns.Bound(x => x.Filiale);
        columns.Bound(x => x.Ruolo);

        //columns.Bound(p => p.).Groupable(false);
        //columns.Bound(p => p.ProductName);
        //columns.Bound(p => p.UnitPrice);
        //columns.Bound(p => p.UnitsInStock);
    })
        .Groupable()
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read =>
            {

                read.Action("GetListaUtenti", "Administrator");
            })
        ).Render();
    %>

In my Controller I've

 public ActionResult GetListaUtenti()
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated) throw new SecurityExeption();

            DataAccessAdministration da = new DataAccessAdministration(Global.IDApplication, IDIstituto);

            int idUser = 1234;
            var res = da.GetListaUtenti(idUser);
            return Json(res);
        }

it's not called :/

## UPDATE 1##
I've used firebug and it tries to reach "NetworkError: 404 Not Found - http://localhost:62866/Administrator/GetListaUtenti"

I think I'm missing somethig for the AdministratorController.... I've added it to the Controllers folder, called it

 public class AdministrationController : Controller
    {
         private int IDIstituto = 1;
        #region IAdministrationService Members

         [HttpPost]
         public ActionResult GetListaUtenti([DataSourceRequest] DataSourceRequest request)
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated) throw new SecurityExeption();

            DataAccessAdministration da = new DataAccessAdministration(Global.IDApplication, IDIstituto);

            int idUser = 1234;
            var res = da.GetListaUtenti(idUser);
            return Json(res);
        }
}

I've no view for Administrator since it's formed of different pages...

Need I to add something to my global.asax?

(now it's

   public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
)

Thanks
0
Accepted
Dimiter Madjarov
Telerik team
answered on 31 Jul 2013, 08:10 AM
Hi Paolo,


It seems that the reason for the issue is a typo in the name of the controller. It is called AdministrationController and in the dataSource configuration it is specified as read.Action("GetListaUtenti", "Administrator").

Another issue might be the returned result. It should be wrapped with the ToDataSourceResult method in order for the paging, sorting, filtering etc. to be applied.
E.g.
return Json(res.ToDataSourceResult(request));

Please let me know if the Grid is working now. I am looking forward to hearing from you.

 

Regards,
Dimiter Madjarov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Michele
Top achievements
Rank 2
answered on 31 Jul 2013, 08:17 AM
Dimiter! res.ToDataSourceResult(request) did the trick!
Thanks a lot!
Tags
ComboBox
Asked by
Michele
Top achievements
Rank 2
Answers by
Dimiter Madjarov
Telerik team
Michele
Top achievements
Rank 2
Share this question
or