0
votes

I encountered a problem as described in the topic.

I use dropDownList from Kendo UI

http://demos.telerik.com/aspnet-mvc/dropdownlist/serverfiltering

that connects with the dataSource(data from database) with Ajax.

Now the dropdownList is getting the data without any problems if I place the method in the same controller as the view. But at first it was placed in another controller and I was getting the data only if I initialized that controller in some other view.

Here is the method:

        public ActionResult GetGroups()
    {
        var list = new List<string>();

        foreach (var item in _repositorySpisGrup.Select())
        {
            list.Add(item.Nazwa);
        }

        return Json(list, JsonRequestBehavior.AllowGet);
    }

Could anyone tell me why that is ?

And to avoid this problem where should I put my action method that may be called from many different views ?

Cheers!

EDIT :

Code for the dropdown (placed in partialView):

            @(Html.Kendo().DropDownList()
            .Name("Group")
            .Value(Model.Group)
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("GetGroups", ViewContext.RouteData.GetRequiredString("controller"));
                })
                .ServerFiltering(true);
            })
                )

Like I said this is a working code because I copied the Action method to the same controller as the view. The problem is when I take Json data from another controller.

1
Whats the code for your dropdown?user3559349
It doesn't work when you use read.Action("GetGroups", "Controller2"); ? Verify your ajax request with Fiddler.nZeus
Everything is fine with you Action method. Check the generated ajax url + ajax requestnZeus
@nZeus eh... I figured out what the problem was. I didn't notice this at first. The action method was placed before in a controller that has [Authorize] atribute and I was calling the method from a place where I was not authorized. Well.. people learn from their mistakes. Anyway thanks for the help :)Mateusz Migała
Ah, ok. I think you should post an answer for your question and mark it as an answernZeus

1 Answers

0
votes

I figured out what the problem was. I didn't notice this at first. The action method was placed before in a controller that has [Authorize] atribute and I was calling the method from a place where I was not authorized. No error was thrown so this was pretty difficult to find.