Hi all,

as in the title problem is how to access models without passing to view . I use telerik controles.
in the following example for gird
if you notice you can see they haven passe employee model to view but in ajax version they havnt passe specific model to view but they use severeal models in the view.

How this thing haplend? can sombody help me.

thx in advance..

Dani AI

Generated

Short answer: the grid HTML can appear while the Ajax fetch that fills it fails. The usual culprits are a wrong controller/action URL, a JavaScript error or version mismatch, the server returning HTML (redirect to login) instead of JSON, or the EF query being deferred (context disposed) so nothing is serialized.

As noted, the Ajax-bound action is expected to return the grid data as JSON. Confirm the Ajax call is actually succeeding by using the browser dev tools (F12) — Network → XHR. The request to your _EmployeesHierarchyAjax endpoint should be 200 and the response must be JSON (not an HTML login page or an error trace). If it is 404/500 or returns HTML, fix the route/auth/exception first. Placing a breakpoint in the action is the fastest check to see if it’s hit.

Useful, quick checks and fixes:

  • Use Url.Action for the Select URL so MVC builds the correct path (and respects Areas):
.DataBinding(d => d.Ajax().Select(Url.Action("_EmployeesHierarchyAjax","Home")))
  • Temporarily materialize the query and return JSON to verify the data payload (helps rule out lazy/EF context issues):
public ActionResult _EmployeesHierarchyAjax()
{
    var list = ctx.activities.Select(a => new ActivityViewModel { /* map fields */ }).ToList();
    return Json(list, JsonRequestBehavior.AllowGet);
}
  • Look at the Console for JS errors and ensure jQuery is loaded before the Telerik scripts (version incompatibility will break data binding). ’s tip about checking jQuery/modernizr is on point.

Checklist: F12 Network + Console, breakpoint in action, try Json(list) to confirm payload, use Url.Action for reliable URLs, confirm authentication isn’t redirecting Ajax requests, and verify Telerik script/assembly versions match.

Recommended Answers

All 10 Replies

Hi,

I tried ajax code.

in the view I tried the following code. first I wanted to render some data without passing a spcific model to view

@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>
@(Html.Telerik().Grid<myOffice.ViewModels.ActivityViewModel>()
    .Name("Employees")
    .Columns(columns =>
    {
        columns.Bound(e => e.Id).Width(140);
        columns.Bound(e => e.Employee).Width(140);
        columns.Bound(e => e.Embassy).Width(200);
        columns.Bound(e => e.Bank).Width(200);

    })

    .DataBinding(dataBinding => dataBinding.Ajax().Select("_EmployeesHierarchyAjax", "Grid"))
    .Pageable(paging => paging.PageSize(5))
    .Scrollable(scrolling => scrolling.Height(580))
    .Sortable()
)

<script type="text/javascript">

function expandFirstRow(grid, row) {
    if (grid.$rows().index(row) == 0) {
        grid.expandRow(row);
    }
}

function employees_onRowDataBound(e) {
    var grid = $(this).data('tGrid');
    expandFirstRow(grid, e.row);
}

function orders_onRowDataBound(e) {
    var grid = $(this).data('tGrid');
    expandFirstRow(grid, e.row);
}
</script> 

but then I get the grid without data.
if you want any more code deal with this please let me know.

hi another problem is contoller.

[GridAction]
        public ActionResult _EmployeesHierarchyAjax()
        {
            var employees = from e in new  kesofficeEntities().activities
                            orderby e.id
                            select new ActivityViewModel
                            {
                                Id=e.id,
                                Employee=(int)e.employee,
                                Embassy=(int)e.embassy,
                                Bank=(decimal)e.bank
                            };

            return View(new GridModel(employees));
        }

in the code they passe new GridModel(employees)
but this object is not catch in the view.
then how data is catch?

hi another problem is contoller.

[GridAction]
        public ActionResult _EmployeesHierarchyAjax()
        {
            var employees = from e in new  kesofficeEntities().activities
                            orderby e.id
                            select new ActivityViewModel
                            {
                                Id=e.id,
                                Employee=(int)e.employee,
                                Embassy=(int)e.embassy,
                                Bank=(decimal)e.bank
                            };

            return View(new GridModel(employees));
        }

in the code they passe new GridModel(employees)
but this object is not catch in the view.
then how data is catch?

Make sure you have the latest versions of jquery, jquery.validate, and modernizr if you're using them. Some of the older versions will cause telerik to break.

in the code they passe new GridModel(employees)
but this object is not catch in the view.
then how data is catch

When the action is marked with the GridAction attribute it returns the GridModel as a JSON object that is used to update the contents of the telerik grid. Since you are using Ajax databinding this is done with an Ajax call to the server.

thx Cherryhomesj for ur reply, I checked the telerick script and content folders, they have folder with name 2012.2.607. Therefore, I can say I have installed latest jquery and css.I downloaded newly telerik plugins 3 weeks ago.

Can you also post the code for your layout page, should be int eh views/shared folder. Also, just to be sure, the name of your controller is GridController?

hi,
thx, why do u specifiy that controler should be Grid.above given code doesnt refer GridControler anywhere. can you explain this point more?

 <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @(Html.Telerik().StyleSheetRegistrar()
        .DefaultGroup(group => group.Add("telerik.common.css")
        .Add("telerik.metro.css"))
        )
        @Styles.Render("~/Content/themes/base/css", "~/Content/css")
        @Scripts.Render("~/bundles/modernizr")

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("Your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>       

                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                            <li>@Html.ActionLink("Test", "Test", "Home")</li>

                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
                <div class="float-right">
                    <ul id="social">
                        <li><a href="http://facebook.com" class="facebook">Facebook</a></li>
                        <li><a href="http://twitter.com" class="twitter">Twitter</a></li>
                    </ul>
                </div>
            </div>
        </footer>

         @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
        @(Html.Telerik().ScriptRegistrar())

    </body>
</html>

DataBinding(dataBinding => dataBinding.Ajax().Select("_EmployeesHierarchyAjax", "Grid"))

The "Grid" in that line is specifying the name of the controller to look for the action in.

Grid"

thanks, I had rename it in to Home since I use Homecontroller.

hi, If you suspect my telerik grid controlers doest work, it's wrong. because I could view the telerik grid in the view but without data. actually problem is not getting data to the grid.... so far, I didnt get good hint, or answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.