This is a migrated thread and some comments may be shown as answers.

[Solved] Stop evaluating foreign key properties

1 Answer 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joseph
Top achievements
Rank 1
Joseph asked on 09 Mar 2012, 12:19 PM
Hi There,
I want to stop the Grid view 'edit' command to evaluating the foreign key properties on editable mode. The one bellow is a sample code of what I'm doing in my custom ORM.

public partial class Instrument
{
    public int Id { get; set; }
    public string CodeBbg { get; set; }
    public string Name { get; set; }
    public string InstrumentType { get; set; }
    public DateTime Created { get; set; }
    [ScaffoldColumn(false)]
    public string Description { get; set; }
 
    private ICollection<int> _collection;
 
    [ScaffoldColumn(false)]
    public virtual ICollection<int> Collection
    {
        get { throw new Exception("The collection is not populated yet"); }
        set { _collection = value; }
    }
 
    private string _notes;
    [ScaffoldColumn(false)]
    public string Notes
    {
        get { throw new Exception("The collection is not populated yet"); }
        set { _notes = value; }
    }
}


The view for the gird view is follows:

@model IEnumerable<TelerikMvcGridTest.Models.Instrument>
@(Html.Telerik().Grid(Model)
        .DataKeys(keys => keys.Add(i => i.Id))
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(instrument => instrument.Id).Width(100);
            columns.Bound(instrument => instrument.CodeBbg).Width(200);
            columns.Bound(instrument => instrument.Name);
            columns.Bound(instrument => instrument.InstrumentType).Width(120);
            columns.Command(commands =>
            {
                commands.Edit();
            }).Width(200);
        })
        .Groupable()
        .DataBinding(dataBinding => dataBinding.Ajax()
                .Update("Update", "Index"))
        .Editable(editing => editing.Mode(GridEditMode.PopUp))
        .Sortable()
        .Pageable()
        .Filterable()
)


I don't want the editor not only just to display the field but also not to evaluate the property. The exception get thrown all the time hence the grid view control access the 'Collection' property of the 'Instrument' model. This happens when I enable editing (i.e. commands.Edit(); ) on read-only grid view the property doesn't get accessed if not bound. Can you please suggest me a way to attribute or anything similar to not to access the property.

It is very crucial on an EntityFramework since the lazy loading Foreign Entities will get loaded from the database unnecessarily. 

FYI: The Html.EditForModel(); works fine for the same Entity class

@using (Html.BeginForm())
{
    @Html.EditorForModel();
}

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 14 Mar 2012, 11:06 AM
Hello Jospeh,

I posted my reply in the support ticket you have opened. For convenience I am pasting it below:

*******************************************************************************************************************

When enabling editing and there is an Ajax action (Update in this case), the Grid uses the JavaScript serializer on the model. You can specify that these properties shouldn't be evaluated by setting the ScriptIgnore attribute on them.
Also, if you wish to use Server binding, in which case the serializer won't be used, you should declare the Update as a Server method. If you want to use Ajax binding, you should also declare the Ajax Select action. Otherwise filtering,sorting and paging won't work.

*******************************************************************************************************************

Greetings,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
Joseph
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or