Hi Guys,
I'm working with mvc and EF + razor.
I'd like to edit a model that is composed like this:
(EF generated from DB first)
Now, I handled all the field with a normal razor view...All but the ICollection<DATACLASS_ATTRIBUTES_T012> DATACLASS_ATTRIBUTES_T012
Here ATTRIBUTEID is the foreign key for DC_ATTRIBUTES_T006 table while UOM_ID is the foreign key for UNITS_OF_MEASURE_T013 table.
in my grid, I'd like to edit theese fields as combobox. I checked the "custom editor" and "foreign key" example but I'm not able to make them work.
following the custom editor ex, when I enter edit mode (inline or incell) the grid shows me ever entity field (like 4-5).
On the other hand, trying with the foreign key example I got an error
Here is my grid
and my read action:
The Error I got is: Value cannot be null.Parameter name: items (the controller's action breakpoint is not hit)
The point is that to me, in this view, attributes and measure units are like enum, I'd like to use them just as a combo with Id-Name tuple...all I need is to have the foreign key field not null on update/create.
Thanks
Fabio
I'm working with mvc and EF + razor.
I'd like to edit a model that is composed like this:
(EF generated from DB first)
public
partial
class
DATACLASS_T003
{
public
DATACLASS_T003()
{
this
.DATACLASS_ATTRIBUTES_T012 =
new
HashSet<DATACLASS_ATTRIBUTES_T012>();
}
public
long
PARENTID {
get
;
set
; }
public
long
CLASSID {
get
;
set
; }
public
Nullable<
long
> DATACLASS_PARENTID {
get
;
set
; }
public
int
GEOMETRY_TYPE {
get
;
set
; }
public
string
NAME {
get
;
set
; }
public
int
ID {
get
;
set
; }
public
virtual
ICollection<DATACLASS_ATTRIBUTES_T012> DATACLASS_ATTRIBUTES_T012 {
get
;
set
; }
public
virtual
TIP_GEOMETRY_T014 TIP_GEOMETRY_T014 {
get
;
set
; }
}
public
partial
class
DATACLASS_ATTRIBUTES_T012
{
public
DATACLASS_ATTRIBUTES_T012()
{
this
.ATT_VALUES_T004 =
new
HashSet<ATT_VALUES_T004>();
}
public
int
ATTRIBUTEID {
get
;
set
; }
public
int
DATACLASS_ID {
get
;
set
; }
public
Nullable<
int
> UOM_ID {
get
;
set
; }
public
virtual
ICollection<ATT_VALUES_T004> ATT_VALUES_T004 {
get
;
set
; }
public
virtual
DATACLASS_T003 DATACLASS_T003 {
get
;
set
; }
public
virtual
DC_ATTRIBUTES_T006 DC_ATTRIBUTES_T006 {
get
;
set
; }
public
virtual
UNITS_OF_MEASURE_T013 UNITS_OF_MEASURE_T013 {
get
;
set
; }
}
Here ATTRIBUTEID is the foreign key for DC_ATTRIBUTES_T006 table while UOM_ID is the foreign key for UNITS_OF_MEASURE_T013 table.
in my grid, I'd like to edit theese fields as combobox. I checked the "custom editor" and "foreign key" example but I'm not able to make them work.
following the custom editor ex, when I enter edit mode (inline or incell) the grid shows me ever entity field (like 4-5).
On the other hand, trying with the foreign key example I got an error
Here is my grid
@(Html.Kendo().Grid(Model.DATACLASS_ATTRIBUTES_T012)
.Name(
"Grid"
)
.ToolBar(commands => { commands.Create().Text(
"New Attribute"
); commands.Save(); })
.Columns(columns =>
{
columns.Bound(p => p.ATTRIBUTEID).Title(
"Attribute"
);
columns.ForeignKey(p => p.UOM_ID, (System.Collections.IEnumerable)ViewData[
"Measure"
],
"ID"
,
"CODE"
)
.Title(
"Measure Unit"
);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Editable(mode => mode.Mode(GridEditMode.InCell))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.ServerOperation(
false
)
.Batch(
true
)
.Model(model =>
{
model.Id(v => v.ATTRIBUTEID);
model.Field(v => v.ATTRIBUTEID).Editable(
false
);
model.Field(v => v.UOM_ID).DefaultValue(1);
})
.Read(r => r.Action(
"GetAttributeByDataClass/"
+ Model.ID,
"Attribute"
))
.Update(r => r.Action(
"UpdateAttribute"
,
"Attribute"
))
)
)
public
ActionResult GetAttributeByDataClass([DataSourceRequest]DataSourceRequest request,
long
Id)
{
IList<DATACLASS_ATTRIBUTES_T012> res = db.DATACLASS_ATTRIBUTES_T012
.Include(x => x.DC_ATTRIBUTES_T006)
.Include(x => x.UNITS_OF_MEASURE_T013)
.Where(x => x.DATACLASS_ID == Id).ToList();
foreach
(DATACLASS_ATTRIBUTES_T012 item
in
res)
{
item.DC_ATTRIBUTES_T006.DATACLASS_ATTRIBUTES_T012 =
null
;
item.UNITS_OF_MEASURE_T013.DATACLASS_ATTRIBUTES_T012 =
null
;
//to avoid circular ref
}
ViewData[
"Attribute"
] = db.DC_ATTRIBUTES_T006;
ViewData[
"AttributeDefault"
] = db.DC_ATTRIBUTES_T006.First();
ViewData[
"Measure"
] = db.UNITS_OF_MEASURE_T013;
DataSourceResult result = res.ToDataSourceResult(request);
return
Json(result);
}
The Error I got is: Value cannot be null.Parameter name: items (the controller's action breakpoint is not hit)
The point is that to me, in this view, attributes and measure units are like enum, I'd like to use them just as a combo with Id-Name tuple...all I need is to have the foreign key field not null on update/create.
Thanks
Fabio