ASP.NET MVC Razor视图引擎上的Telerik Rad Grid - DetailView最初没有填充

时间:2011-05-15 23:29:52

标签: asp.net-mvc asp.net-mvc-3 razor grid telerik

好的......我花了太多时间在这个上挣扎,所以我把它传递给了专家 - > YOU。

我非常简单的ASP.NET MVC(带Razor View Engine的v3)页面使用Telerik Rad Grid控件显示某些类型列表,然后我在网格的DetailsView中显示相关代码。

做人口很容易。我有一个ViewModel用于我的TypeCodeList类型,并将其发送到强类型视图以填充网格。这很有效......并且网格看起来很棒 - 感谢Telerik。但是,我添加了DetailsView,然后以相同的方式填充子TypeCodes。不好的是,当我的网格填充时,我选择左边的三角形来展开树并看到子记录,没有任何东西。但是,如果我选择网格底部的“刷新”按钮,那么我可以点击三角形并显示子记录。

因此(总结),子记录不会显示在初始加载上。只有当我选择网格的AJAX刷新时,我才能得到孩子们。否则,它会按要求运行。

我一直试图看看我是否可以在页面加载时通过javascrip以编程方式启动刷新。或者如果我可以在没有先刷新的情况下选择自己填充的东西 - 这将是更好的选择。

以下是我的代码:

相关控制器代码(我已经取出了更新,删除,插入,记录和数据访问方法)

[HandleErrorWithElmah]
public partial class HostController : Controller
{

    /// <summary>
    /// Index - Home for HostController
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
        return View();
    }

    #region Type List Section

    /// <summary>
    /// Gets the list of TypeLists - yea...say that again
    /// </summary>
    [GridAction]
    public ActionResult TypeCodeList()
    {

        var model = GetActiveTypeLists();

        // Get all of the type lists and send them to the view
        return View(model);

    }


    /// <summary>
    /// The ajaxified Select
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs(HttpVerbs.Post)]
    [GridAction]
    public ActionResult _TypeCodeList()
    {

        var model = GetActiveTypeLists();
        return Json(new GridModel(model));


    }

    /// <summary>
    /// Simply a wrapper to get all of the current type list values.
    /// </summary>
    /// <returns></returns>
    private IEnumerable<TypeCodeListViewModel> GetActiveTypeLists()
    {

        var model = from p in entityRepository.Find<TypeList>(p => p.IsActive == true)
                    select new TypeCodeListViewModel
                    {
                        TypeListId = p.TypeListId,
                        Name = p.Name,
                        Description = p.Description,
                        IsActive = p.IsActive
                    };


        return model;

    }

    #endregion

    #region Type Code Section

    [AcceptVerbs(HttpVerbs.Post)]
    [GridAction]
    public ActionResult _TypeCodeForTypeListAjax(int typeListId)
    {
        var model = GetActiveTypeCodes(typeListId);
        return Json(new GridModel(model));
    }


    /// <summary>
    /// Simply a wrapper to get all of the current type Code values.
    /// </summary>
    /// <returns></returns>
    private IEnumerable<TypeCodeViewModel> GetAllActiveTypeCodes()
    {

        var model = from p in entityRepository.Find<OurLeaguePlay.Models.TypeCode>(p => p.IsActive == true).OrderBy(ord => ord.CodeName)
                    select new TypeCodeViewModel
                    {
                        TypeCodeId = p.TypeCodeId,
                        TypeListId = p.TypeListId,
                        CodeName = p.CodeName,
                        CodeValue = p.CodeValue,
                        Description = p.Description,
                        IsActive = p.IsActive
                    };


        return model;

    }


    /// <summary>
    /// Simply a wrapper to get all of the current type Code values.
    /// </summary>
    /// <returns></returns>
    private IEnumerable<TypeCodeViewModel> GetActiveTypeCodes(int typeListId)
    {

        var model = from p in entityRepository.Find<OurLeaguePlay.Models.TypeCode>(p => p.IsActive == true && 
                                                                                        p.TypeListId == typeListId).OrderBy(ord => ord.CodeName)
                    select new TypeCodeViewModel
                    {
                        TypeCodeId = p.TypeCodeId,
                        TypeListId = p.TypeListId,
                        CodeName = p.CodeName,
                        CodeValue = p.CodeValue,
                        Description = p.Description,
                        IsActive = p.IsActive
                    };


        return model;

    }


    #endregion

}

这是我的观看代码: (我已经完成了所有失败的javascript尝试尝试强制加载页面加载。)

    @model IEnumerable<TypeCodeListViewModel>
@using Telerik.Web.Mvc.UI
@using Telerik.Web.Mvc
@using OurLeaguePlay.ViewModels
@{Html.Telerik().Grid<TypeCodeListViewModel>(Model)
        .Name("TypeLists")
        .DetailView(details => details.ClientTemplate(
            Html.Telerik().Grid<TypeCodeViewModel>()
                .Name("TypeCode_<#= TypeListId #>")
                .DataKeys(keys => keys.Add(k => k.TypeCodeId))
                .Columns(columns =>
                {
                    columns.Bound(o => o.CodeName).Width(40);
                    columns.Bound(o => o.CodeValue).ReadOnly(true).Width(40);
                    columns.Bound(o => o.Description).Width(100);
                })
                .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax().Select("_TypeCodeForTypeListAjax", "Host", new { typeListId = "<#= TypeListId #>" })
                                          .Enabled(true);
                    }
                    )
                .Pageable()
                .Sortable()
                .NoRecordsTemplate("No Type Codes exist for the selected Type List")
                .ToHtmlString()
            )
        )
        .DataKeys(keys => keys.Add(k => k.TypeListId))
        .Columns(columns =>
        {
            columns.Bound(o => o.Name).Width(100);
            columns.Bound(o => o.Description).Width(150);
            columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Image);
                commands.Delete().ButtonType(GridButtonType.Image);
            }
                           ).Width(30);
        })
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax().Select("_TypeCodeList", "Host")
                              .Update("UpdateTypeList", "Host")
                              .Insert("InsertTypeList", "Host")
                              .Delete("DeleteTypeList", "Host")
                              .Enabled(true);
            dataBinding.Server().Select("TypeCodeList", "Host", new { ajax = ViewData["ajax"] });
        }
        )
        .Editable(editable => editable.Enabled(true).Mode(GridEditMode.InLine))
        .Pageable(page => page.PageSize(10))
        .Sortable()
        .Selectable()
        .Scrollable(scroll => scroll.Enabled(false))
        .NoRecordsTemplate("No Type Lists can be retrieved from the database")
        .ToolBar(commands => commands.Insert())
        .Render();
}

最后......这里是ViewModel类:

public class TypeCodeListViewModel 
{

    [ScaffoldColumn(false)]
    public int TypeListId { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(25, ErrorMessage = "Max Length is 25")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(25, ErrorMessage="Max Length is 25")]
    public string Description { get; set; }

    [ScaffoldColumn(false)]
    public bool IsActive { get; set; }


}

public class TypeCodeViewModel
{

    [ScaffoldColumn(false)]
    public int TypeCodeId { get; set; }

    [ScaffoldColumn(false)]
    public int TypeListId { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(25, ErrorMessage = "Max Length is 25")]
    [DisplayName("Name")]
    public string CodeName { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(25, ErrorMessage = "Max Length is 25")]
    [DisplayName("Value")]
    public string CodeValue { get; set; }

    [StringLength(500, ErrorMessage = "Max Length is 500")]
    public string Description { get; set; }

    [ScaffoldColumn(false)]
    public bool IsActive { get; set; }

}

1 个答案:

答案 0 :(得分:0)

嗯......我想我自己弄明白了...这就像让网格绑定自己一样简单而不是通过非ajax方法强制数据进入它,在初始显示时调用页。

Public ActionResult TypeCodeList()

功能应该简单地更新为以下内容:

Public ActionResult TypeCodeList()
{
    return View();
}

没有[GridAction]装饰器。

如果不强制将值放入网格中,它将使用Ajax方法绑定自身,然后子网格将在扩展时填充。