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 http://demos.telerik.com/aspnet-mvc/razor/grid/hierarchyserverside
if you notice you can see they haven passe employee model to view but in ajax version http://demos.telerik.com/aspnet-mvc/razor/grid/hierarchyajax 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..

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.