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

New version: Hidden Guid Id defaults to empty string instead of Guid.Empty

7 Answers 1188 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 25 Sep 2012, 06:19 PM
After updating to 2012.2.913, our updatable grids stopped being able to save records to the data source.  ModelState.IsValid was returning false for "Id", saying "The Id field is required".  The Id field is a Guid which is auto-populated by our NHibernate data model after a record is added, so we found this puzzling.

Our simplest grids were like this:

@(Html.Kendo().Grid<VMMudType>()
      .Name("vMudTypeGrid")
      .DataSource(datasource => datasource
                                    .Ajax()
                                    .Model(model => model.Id(m => m.Id))
                                    .Create(r => r.Action("Insert", "MudTypeGrid"))
                                    .Read(r => r.Action("Select", "MudTypeGrid"))
                                    .Update(r => r.Action("Update", "MudTypeGrid"))
                                    .Destroy(r => r.Action("Delete", "MudTypeGrid"))
      )
      .Columns(columns =>
      {
          columns.Bound(a => a.Name);
          columns.Command(command =>
          {
              command.Edit();
              command.Destroy();
          });
      })
      .Editable(ed => ed.Mode(GridEditMode.InLine))
      .Sortable()
      .Filterable()
      .ToolBar(commands => commands.Create())
      )

Data model had only the two fields, "Id", which was a Guid, and "Name", which was a string.

Looking at the actual save requests for the pre-upgrade grid versus the upgraded version in Fiddler, I saw the following difference in the POST results: The old version sent "sort=&group=&filter=&Id=00000000-0000-0000-0000-000000000000&Name=Water-Based", while the new version sent "sort=&group=&filter=&Id=&Name=Water-Based".  The generated Javascript for the grid also showed this difference: model:{id:"Id",fields:{Name:{type:"string"},Id:{type:"object",defaultValue:"00000000-0000-0000-0000-000000000000"}}} for the older version, vs. "model":{"id":"Id","fields":{"Name":{"type":"string"},"Id":{"type":"string"}}} for the new version.

The change that broke things seems to have been in TypeExtensions.cs, where ToJavaScriptType(this type) had a new clause for Guids:

if (type.GetNonNullableType() == typeof(Guid))
{
    return "String";
}

There are workarounds for this.  I changed our Insert grid controller to specifically not bind on Id:

public virtual ActionResult Insert([DataSourceRequest] DataSourceRequest request, [Bind(Exclude = "Id")] TViewModel vm)

It was also possible to work around this by changing ".Model(model => model.Id(m => m.Id))" to
                                    .Model(model =>
                                    {
                                        model.Id(m => m.Id);
                                        model.Field(m => m.Id).DefaultValue(Guid.Empty);
                                    })

But the former will work better with our framework, because we can put it into our grid controller parent class.

I could also have made the change to TypeExtensions to take out the Guid clause, but without knowing more about the rationale for the change, I didn't think I should.  What was the reason for having Javascript treat Guids like strings, and would there be a way to do this which didn't break things when adding new records with Guids that are auto-populated by the data model?

Aaron Humphrey
Pleasant Solutions

7 Answers, 1 is accepted

Sort by
0
Ryan
Top achievements
Rank 1
answered on 27 Sep 2012, 12:41 PM
I am having a similar issue with this version.

My inlide editable grid is sending back zero for the ID when doing an update on the row.  Is this a problem with the new version? Batch ID is always zero when sent to my Batch_Update.


@model IList<CashTransactionBatch>
           
 
@(Html.Kendo().Grid(Model)
    .Name("cashbatches")
    .HtmlAttributes(new { style = "width:700px" })
    .Columns(columns =>
    {
        columns.Bound(r => r.Id).Title("Batch ID");
        columns.Bound(r => r.User).Title("Name");
        columns.Bound(r => r.CreatedDate).Title("Created").Format("{0:M/d/yyyy HH:mm:ss}");
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
 
    })
    .ToolBar(toolbar => toolbar.Create().Text("New Batch"))
    .Editable(editable => editable.Mode(GridEditMode.InLine))        
    .Pageable()
    .Scrollable()
    .Sortable()
    .Selectable()
    .DataSource(dataSource => dataSource
            .Ajax().PageSize(100).ServerOperation(false)
            .Read(read => read.Action("Batch_Read""Cash"))
            .Create(read => read.Action("Batch_Create""Cash"))
            .Update(update => update.Action("Batch_Update""Cash"))
            .Destroy(update => update.Action("DeleteBatch""Cash"))
            .Model(model =>
            {
                model.Id(b => b.Id);
                model.Field(b => b.CreatedDate).Editable(false);
            })
     )
)
0
Petur Subev
Telerik team
answered on 28 Sep 2012, 01:11 PM
Hi guys,

Did you try to set DefaultValue for that field? You can do it the following way:
.Model(model => {
               model.Id(p => p.ProductID);
               model.Field(id => id.ProductID).DefaultValue("0000....");
           })

Let me know if it helps.

Regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
John
Top achievements
Rank 1
answered on 17 Oct 2012, 03:01 PM
I am having the exact same issue as Ryan.  I have an editable grid for a list of very simple entities that have a GUID for the unique key.  When I attempt to perform either an update or a delete, the action fails because the GUID that gets passed to my action method is always the default value for a GUID (all zeros) rather than the actual GUID of the entity.  I verified this by inspecting the URL posted to my action method through Fiddler.  The "DefaultValue" solution does not solve this problem because the issue is on an update or delete, not create.

Note that this issue occurs even if the GUID is not designated to be the unique identifier for the entity, so it may be a problem with posting GUIDs in general.  I am using the current release (2012.2.913).
0
Ryan
Top achievements
Rank 1
answered on 17 Oct 2012, 03:30 PM
I never did solve this problem.  I removed inline editing in the grid where I encountered this problem.

In other grids I am manually sending the row that needs to be deleted or updated to my controller with ajax. 

As John mentioned default value does not solve the problem because it is on update and delete when we need the already existing ID to be sent to the controller.

Ryan
0
jonathan
Top achievements
Rank 1
answered on 19 Nov 2012, 08:09 PM
I have tried using default value as shown below, and I receive cannot convert GUID to string.  My scenerio:
Model:
        [DataMember]
        public Guid Id { get; set; }
 
cshtml:
                    model.Id(p => p.Id);
                    model.Field(p => p.Id).DefaultValue(new Guid());

Am I doing something wrong?

 

 

 

0
Henrik
Top achievements
Rank 1
answered on 15 Mar 2013, 08:57 AM
UPDATE:
It was due to an error not related to Kendo.
Sorry for the inconvenience.
You may delete this post.

I have the exact same problem. When trying to bind a property (Id) of data type Guid like this:
...
columns.Bound(a => a.Id).Visible(false);
...
I get the following error message:

Cannot convert lambda expression to type 'string' because it is not a delegate type

Anyone fixed this?

0
Marcel Härry
Top achievements
Rank 1
answered on 15 Feb 2014, 02:45 AM
Yes Petur, thank you !!!
Tags
Grid
Asked by
Aaron
Top achievements
Rank 1
Answers by
Ryan
Top achievements
Rank 1
Petur Subev
Telerik team
John
Top achievements
Rank 1
jonathan
Top achievements
Rank 1
Henrik
Top achievements
Rank 1
Marcel Härry
Top achievements
Rank 1
Share this question
or