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

Id field is required even though not part of the grid

3 Answers 580 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nicklas
Top achievements
Rank 1
Nicklas asked on 27 May 2014, 11:49 AM
I'm working with a grid in asp.net mvc, where I have a model containing several properties. One of its properties (Id) is a Guid. Whenever I try to create a new row in my grid, entering all details (Id is automatically generated by a backend server when saved) and press "Update", I get an error saying "The Id field is required". Is there a way to get around this?
I suspect the Guid-type to be the one causing trouble here.
Here's my grid:
@(Html.Kendo().Grid<ToolModel>()
      .Name("grid")
      .Columns(columns =>
      {
          columns.Bound(p => p.Displace);
          columns.Bound(p => p.FishingNectOd);
          columns.Bound(p => p.Length);
          columns.Bound(p => p.Model);
          columns.Bound(p => p.Name);
          columns.Bound(p => p.Supplier).Width(150);
          columns.Bound(p => p.TagId);
          columns.Bound(p => p.ToolOd);
          columns.Bound(p => p.Type);
          columns.Bound(p => p.Weight);
          columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
      })
      .ToolBar(toolbar => toolbar.Create())
      .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ToolPopUpTemplate"))
      .Pageable()
      .Sortable()
      .Scrollable()
      .HtmlAttributes(new { style = "height:500px;" })
      .DataSource(dataSource => dataSource
          .Ajax()
          .PageSize(10)
          .Events(events => events.Error("error_handler"))
          .Model(model => model.Id(toolModel => toolModel.Weight))
          .Create(update => update.Action("EditingPopup_Create", "ToolManagement"))
          .Read(read => read.Action("EditingPopup_Read", "ToolManagement"))
          .Update(update => update.Action("EditingPopup_Update", "ToolManagement"))
          .Destroy(destroy => destroy.Action("EditingPopup_Destroy", "ToolManagement"))
      ))
Template:
@using Services.Models.Tool
@model ToolModel
 
<div class="editor-label">
    @(Html.LabelFor(model => model.Displace))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Displace))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.FishingNectOd))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.FishingNectOd))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Length))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Length))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Model))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Model))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Name))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Name))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Supplier))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Supplier))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.TagId))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.TagId))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.ToolOd))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.ToolOd))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Type))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Type))
</div>
<div class="editor-label">
    @(Html.LabelFor(model => model.Weight))
</div>
<div class="editor-field">
    @(Html.TextBoxFor(model => model.Weight))
</div>

Controller:
public class ToolManagementController : Controller
{
    private static readonly ILog Log = LogManager.GetLogger("ToolManagementController.class");
    public ToolServiceClient ToolService { get; set; }
 
    public ToolManagementController()
    {
        try
        {
            ToolService = new ToolServiceClient();
        }
        catch (Exception e)
        {
            Log.Error(e);
        }
    }
 
    // GET: ToolManagement
    public ActionResult Tools()
    {
        return View();
    }
 
    public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
    {
        ToolContract[] list = ToolService.GetTools();
        return Json(list.ToDataSourceResult(request));
    }
 
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, ToolModel tool)
    {
        if (tool != null && ModelState.IsValid)
        {
            ToolService.CreateTool(new ToolContract
            {
                Displace = tool.Displace,
                FishingNectOD = tool.FishingNectOd,
                Length = tool.Length,
                Model = tool.Model,
                Name = tool.Name,
                Supplier = tool.Supplier,
                TagId = tool.TagId,
                ToolOD = tool.ToolOd,
                Type = tool.Type,
                Weight = tool.Weight
 
            });
        }
 
        return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
    }
 
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, ToolModel tool)
    {
        if (tool != null && ModelState.IsValid)
        {
            ToolService.UpdateTool(new ToolContract
            {
                Displace = tool.Displace,
                FishingNectOD = tool.FishingNectOd,
                Length = tool.Length,
                Model = tool.Model,
                Name = tool.Name,
                Supplier = tool.Supplier,
                TagId = tool.TagId,
                ToolOD = tool.ToolOd,
                Type = tool.Type,
                Weight = tool.Weight,
                Id = tool.Id
            });
        }
 
        return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
    }
 
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, ToolModel tool)
    {
        if (tool != null)
        {
            ToolService.DeleteTool(tool.Id);
        }
 
        return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
    }
 
}

Finally, the model:
public ToolModel(ToolContract toolContract)
{
    ToolContract = toolContract;
}
 
public ToolModel()
{
}
 
private ToolContract ToolContract
{
    get { return _toolContract ?? (_toolContract = new ToolContract()); }
    set { _toolContract = value; }
}
 
 
public double? Displace
{
    get { return ToolContract.Displace; }
    set { ToolContract.Displace = value; }
}
 
 
public double? FishingNectOd
{
    get { return ToolContract.FishingNectOD; }
    set { ToolContract.FishingNectOD = value; }
}
 
[ScaffoldColumn(false)]
public Guid Id
{
    get { return ToolContract.Id; }
    set { ToolContract.Id = value; }
}
 
public double? Length
{
    get { return ToolContract.Length; }
    set { ToolContract.Length = value; }
}
 
public string Model
{
    get { return ToolContract.Model; }
    set { ToolContract.Model = value; }
}
 
public string Name
{
    get { return ToolContract.Name; }
    set { ToolContract.Name = value; }
}
 
public string Supplier
{
    get { return ToolContract.Supplier; }
    set { ToolContract.Supplier = value; }
}
 
public string TagId
{
    get { return ToolContract.TagId; }
    set { ToolContract.Supplier = value; }
}
 
[Display(Name = "Outer Diameter")]
public double? ToolOd
{
    get { return ToolContract.ToolOD; }
    set { ToolContract.ToolOD = value; }
}
 
public string Type
{
    get { return ToolContract.Type; }
    set { ToolContract.Type = value; }
}
 
public double? Weight
{
    get { return ToolContract.Weight; }
    set { ToolContract.Weight = value; }
}
}

3 Answers, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 29 May 2014, 08:56 AM
Hi Nicklas,

Thank you for the provided code snippets. I reviewed them and noticed that the Id field's value is not specified in the EditingPopup_Create method, which probably is the reason for that error. Additionally, you could set a default value for the Id field in the DataSource's Model.

Regards,
Alexander Popov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Matt
Top achievements
Rank 1
Veteran
answered on 19 Jan 2019, 06:41 PM
Hi Alexander. How about an example of how you would do that?
0
Viktor Tachev
Telerik team
answered on 23 Jan 2019, 08:00 AM
Hello Matt,

Setting a default value in the DataSource can be done in the Model settings. 

.DataSource(dataSource => dataSource
     
    //...
     
    .Model(model => {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).DefaultValue(-1);
    })
     
    // ...
     
)


Also the Create ActionMethod should return the new item with an ID that will usually be set by the database. The value of that ID should be different than the DefaultValue. For more information and examples on implementing CRUD operations in the Grid check out the resources below:



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.
Tags
Grid
Asked by
Nicklas
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Matt
Top achievements
Rank 1
Veteran
Viktor Tachev
Telerik team
Share this question
or