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

Ignore model property on post to action

4 Answers 783 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sebastian
Top achievements
Rank 1
Sebastian asked on 23 Oct 2018, 12:01 PM

Is there a way to ignore a model property when posting to an action from a grid? Delete, create, update etc. The property needs to be loaded with the grid in the read action but not posted to the delete, create and update actions.

4 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 26 Oct 2018, 08:01 AM
Hello Sebastian,

By default the entire Model will be submitted to the server when updating/creating/deleting an item. However, if you would like to prevent the users from editing a specific field you can configure the Editable setting for it in the DataSource Model configuration. The code would look similar to the following:


.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).Editable(false);
        model.Field(p => p.UnitPrice).Editable(false);
    })
    .PageSize(20)
    .Create(update => update.Action("CreateAction", "Controller"))
    .Read(read => read.Action("ReadAction", "Controller"))
    .Update(update => update.Action("UpdateAction", "Controller"))
    .Destroy(update => update.Action("DestroyAction", "Controller"))
)

Let me know how this approach works for you.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Eli
Top achievements
Rank 1
Veteran
answered on 28 Jan 2019, 04:14 PM

If the field is a complex object how would you keep it from being submitted to server or have it ignored?  This type of data displays fine and I am able to update, however, if I add Create to the Grid it throws an "Uncaught ReferenceError: CreatedBy is not defined"  I have tried a few ways to do this without any luck.  The Add New Record button will not work.  If I comment out the field it all works correctly.  I only want this nested field for Read Display.

For instance if you have 

.Columns(columns =>
            {                              
                columns.Bound(c => c.CreatedBy.DisplayName);
}
 
.Model(model =>
                {
                    model.Field(c => c.CreatedBy.DisplayName).Editable(false);
}
0
Viktor Tachev
Telerik team
answered on 31 Jan 2019, 11:27 AM
Hi Eli,

When creating a new item in the Grid the respective editors would be populated with the defailt value for each field. However, when a field is an object the editor has no way of knowing what value should be applied to it. 

In this case you can specify a default value for the field in the Schema Model configuration. The custom editor example below has a default value specified for the Category field.




Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Eli
Top achievements
Rank 1
Veteran
answered on 01 Feb 2019, 12:59 PM
Thanks Viktor, this helped me get it worked out. 
Tags
Grid
Asked by
Sebastian
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Eli
Top achievements
Rank 1
Veteran
Share this question
or