Telerik-MVC 网格显示属性值与编辑值

发布于 2024-12-01 17:50:45 字数 891 浏览 1 评论 0原文

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

使用 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 post 来发送 PersonId。

救命!

Hopefully this will have a simple answer.

Using MVC3, I am passing a simple list of POCO objects as a model to my view:

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; }
}

I use the PeopleId as the FK property to the Person entity, and the Person navigational property to navigate to the object. Here is my controller for the view:

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

Pretty trivial. I add the columns to the grid and normal edit modes, etc in the view, but for the PersonId property.

QUESTION ABOUT COLUMNS: How can I get the select (normal) mode to display the model.Person.Name, but keep the edit mode on editing model.PersonId? For model binding purposes, I need the HTTP post to send PersonId.

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2024-12-08 17:50:45

简单

如果您在点击编辑时需要的只是 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)
            }
        });
     }
}

Simple

If all you need is the Person.Id when you hit edit (you're editing outside the grid or something), then it IS that simple. Your column would be:

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

And you can get the Id for the person:

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

Full

However, if you're trying to edit in the grid using a combobox, your column should look like:

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

your editor template:

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

Your client script:

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)
            }
        });
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文