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:
Template:
Controller:
Finally, the model:
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"
))
))
@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; }
}
}