DropDownList

2 posts, 0 answers
  1. DSI
    DSI avatar
    2 posts
    Member since:
    Sep 2013

    Posted 12 Oct 2013 Link to this post

    Hello, I'm new with MVC & Kendo and follow some demo videos (http://www.youtube.com/watch?v=1CPediqnIt8) and tried "do-it yourself".

    The Controller:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace KendoUIMvcApplication3.Controllers
    {
        public class FoldersController : Controller
        {
            private FotosACSEntities db = new FotosACSEntities();

            //
            // GET: /Folders/

            public ActionResult Index()
            {
                var folders = db.Folders.Select(f => new
                {
                    ID = f.ID,
                    Nome = f.Nome
                });

                return Json(folders, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); 
    ...

    The View:

    @model IEnumerable<KendoUIMvcApplication3.Folders>

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>
    <br />

    @Html.Kendo().DropDownList().Name("cbFolders").DataSource(dataSource => dataSource.Read(read => read
                .Action("Index", "Folders"))).DataTextField("Nome").DataValueField("ID");

    ...

    And the Result: (IE and Chrome)

    [{"ID":1,"Nome":"\\CD 06_Fotos Pessoais"},{"ID":2,"Nome":"\\Fotos Pessoais\\Album 002\\JPG"},{"ID":3,"Nome":"\\Fotos Pessoais\\Album 003\\JPG"},{"ID":4,"Nome":"\\Fotos Pessoais\\Album 004\\JPG"},{"ID":5,"Nome":"\\Fotos Pessoais\\Album 007\\JPG"},{"ID":6,"Nome":"\\Fotos Pessoais\\Album 008\\JPG"},{"ID":7,"Nome":"\\Fotos Pessoais\\Album 009\\JPG"},{"ID":8,"Nome":"\\Fotos Pessoais\\Album 010\\JPG"},{"ID":9,"Nome":"\\Fotos Pessoais\\Album 011\\JPG"},{"ID":10,"Nome":"\\Fotos Pessoais\\Album 013\\JPG"},{"ID":11,"Nome":"\\Fotos Pessoais\\Album 014\\JPG"},{"ID":12,"Nome":"\\Fotos Pessoais\\Album 017\\JPG"},{"ID":13,"Nome":"\\Fotos PM\\Album 001"},{"ID":14,"Nome":"\\Fotos PM\\Album 002"},...

    What's happening?

    Regards,

    Rui
  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 16 Oct 2013 Link to this post

    Hello Rui,

    You should return a ViewResult and not a JsonResult from the action that renders the View. A separate action method should be used to return JSON for the Ajax request. If you wish to return the data from the Index action then you should still return a ViewResult and bind the data to the DropDownList with the BindTo method. Check this documentation topic which describes both scenarios.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top