【问题标题】:Telerik-MVC Grid Display value of property versus edit valueTelerik-MVC Grid 显示属性值与编辑值
【发布时间】:2011-11-04 03:10:12
【问题描述】:

希望这会有一个简单的答案。

使用 MVC3,我将一个简单的 POCO 对象列表作为模型传递给我的视图:

public partial class PeopleAddress
{
    public int Id { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public int PersonId { get; set; }

    public virtual Person Person { get; set; }
}

我使用 PeopleId 作为 Person 实体的 FK 属性,并使用 Person 导航属性导航到对象。这是我的视图控制器:

public ViewResult Index()
    {
        var peopleaddresses = db.PeopleAddresses.Include("Person");
        return View(peopleaddresses.ToList());
    }

相当琐碎。我将列添加到视图中的网格和普通编辑模式等,但对于 PersonId 属性。

关于列的问题:如何获得选择(正常)模式来显示 model.Person.Name,但在编辑 model.PersonId 时保持编辑模式?出于模型绑定的目的,我需要 HTTP 帖子来发送 PersonId。

救命!

【问题讨论】:

    标签: model-view-controller asp.net-mvc-3 telerik telerik-grid telerik-mvc


    【解决方案1】:

    简单

    如果您在点击编辑时只需要 Person.Id(您在网格之外进行编辑或其他内容),那么就这么简单。您的专栏将是:

    columns.Bound(e => e.Person).Title("Person").ClientTemplate("<#= Person ? Person.Name : '' #>");
    

    您可以获取此人的 ID:

    onEdit(e) {
        var personId = e.dataItem['Person'].Id;
    }
    

    完整

    但是,如果您尝试使用组合框在网格中进行编辑,您的列应如下所示:

    columns.Bound(e => e.Person).Title("Person").ClientTemplate("<#= Person ? Person.Name : '' #>").EditorTemplateName("PersonSelector");
    

    您的编辑器模板:

    @(Html.Telerik().ComboBox()
    .Name("YourNameGoesHere")
    .DataBinding(binding => binding.Ajax().Select("SelectPeopleForComboBox","Shared")))
    

    您的客户端脚本:

    onEdit(e){
        $comboBox = $(e.cell).find('#Person');
        if($comboBox.length > 0) {
            var comboBox = $ddl.data('tComboBox');
            comboBox.fill(function(){
                if (e.dataItem['Person'] != null){
                        ddl.value(e.dataItem['Person'].Id)
                }
            });
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多