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

DropDownList

1 Answer 88 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
DSI
Top achievements
Rank 1
DSI asked on 12 Oct 2013, 04:09 PM
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

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Oct 2013, 07:36 AM
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!
Tags
DropDownList
Asked by
DSI
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or