So I am trying to do something a little odd. I have a few tables with denormalized data. I have a setup table that based on the field name returns a list of values I populate a combobox on my grid with. On my grid when I edit up create a new record it never sends over the selected value. Also, by default if that record in the grid does not have a value it says "null" instead of the placeholder which seems to defeat the whole purpose of a place holder. Any assistance would be greatly appreciated.
public class InventoryViewModel
{
public int InventoryId { get; set; }
public int ProjectId { get; set; }
public int Qty { get; set; }
public bool AuditFlag { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDt { get; set; }
public int? ProductNameId { get; set; }
public string Size { get; set; }
public string WorkTypes { get; set; }
[DisplayName("Size")]
[UIHint("NewMeterSizeDDEditor")]
public MeterSizeDDViewModel SizeList { get; set; }
[DisplayName("Work Types")]
[UIHint("WorkTypesMSEditor")]
public IEnumerable<InventoryWorkTypesMSViewModel> WorkTypesMS { get; set; }
public List<InventoryViewModel> GetList(int projectId, int? productNameId)
{
try
{
int ii = 1;
List<InventoryViewModel> model = new List<InventoryViewModel>();
model = _db.Inventories
.Select(i =>
new InventoryViewModel
{
InventoryId = i.InventoryId,
ProjectId = i.ProjectId,
Qty = i.Qty,
AuditFlag = i.AuditFlag,
ProductNameId = i.ProductNameId,
UpdatedBy = i.UpdatedBy,
UpdatedDt = i.UpdatedDt,
Size = i.Size,
WorkTypes = i.WorkTypes,
SizeList = new MeterSizeDDViewModel
{
// WorkOrderFieldDropDownValueId = -1,
Size = i.Size
}
})
.Where(i => i.ProjectId == projectId)
.Where(i => productNameId == 0 || i.ProductNameId == productNameId).ToList();
foreach(var m in model)
{
if(m.WorkTypes != null)
{
string[] workTypeArray = m.WorkTypes.Split(',');
List<InventoryWorkTypesMSViewModel> f = new List<InventoryWorkTypesMSViewModel>();
foreach(string wt in workTypeArray)
{
f.Add(new InventoryWorkTypesMSViewModel
{
Id = ii,
WorkType = wt
});
ii++;
}
m.WorkTypesMS = f.AsEnumerable();
}
}
return model.ToList();
}
catch (Exception ex)
{
StatusErrorHandlingViewModel sc = new StatusErrorHandlingViewModel();
System.Web.UI.Page page = new System.Web.UI.Page();
string loginUserID = page.User.Identity.Name;
sc.Id = -1;
sc.Class = "InventoryViewModel";
sc.Method = "GetList";
sc.User = loginUserID;
sc.Message = ex.ToString();
sc.AddError(sc);
_db.SaveChanges();
return null;
}
}
public JsonResult _PopulateNewMeterSize(string text)
{
var meterSizes = _db.WorkOrderFieldDropDownValues
.Where(v => v.WorkOrderFieldDropDown.WorkOrderField == "NewSize")
.Select(w => new MeterSizeDDViewModel
{
// WorkOrderFieldDropDownValueId = w.WorkOrderFieldDropDownValueId,
Size = w.Value
});
if (!string.IsNullOrEmpty(text))
{
meterSizes = meterSizes.Where(p => p.Size.Contains(text));
}
return Json(meterSizes, JsonRequestBehavior.AllowGet);
}
public void _PopulateNewMeterSizeViewData()
{
var meterSizes = _db.WorkOrderFieldDropDownValues
.Where(v => v.WorkOrderFieldDropDown.WorkOrderField == "NewSize")
.Select(w => new MeterSizeDDViewModel
{
// WorkOrderFieldDropDownValueId = w.WorkOrderFieldDropDownValueId,
Size = w.Value
}); //.OrderBy(v => v.WorkOrderFieldDropDownValueId).ToList();
ViewData["newSize"] = meterSizes;
ViewData["defaultNewSize"] = meterSizes.First();
}
View
<script type="text/kendo" id="WorkTypesMSTemp">
<ul>
#for(var i = 0; i< data.length; i++){#
<li>#:data[i].WorkType#</li>
#}#
</ul>
</script>
<script type="text/javascript">
var WorkTypesMSTemp = kendo.template($("#WorkTypesMSTemp").html(), { useWithBlock: false });
</script>
<h2>Inventory List</h2>
@(Html.Kendo().Grid<FS2.ViewModels.Inventory.InventoryViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(u => u.InventoryId).Visible(false);
columns.Bound(u => u.ProjectId).Visible(false);
columns.Bound(u => u.ProductNameId).Visible(false);
columns.Bound(u => u.Qty);
columns.Bound(u => u.AuditFlag);
columns.Bound(u => u.SizeList).ClientTemplate("#=SizeList.Size#");
columns.Bound(u => u.WorkTypesMS).ClientTemplate("#=WorkTypesMSTemp.WorkTypesMS#");
columns.Command(command => { command.Destroy(); }).Width(150);
})
.ToolBar(toolBar =>
{
//code to create template
toolBar.Template(@<text>
<div class="toolbarbuttons">
<a id="AddNew" class='k-button k-button-icontext k-grid-add k-state-disabled' href='#'><span class='k-icon k-add'></span>Add new record</a>
<a class='k-button k-button-icontext k-grid-save-changes' href='#'><span class='k-icon k-i-check'></span>Save</a>
@*onclick="return onSave()"*@
</div>
<div class="toolbar">
<label class="workDate-label" for="workDate">Product:</label>
@(Html.Kendo().DropDownList()
.Name("productname")
.OptionLabel("All")
.DataTextField("ProductName")
.DataValueField("ProductNameId")
.AutoBind(false)
.Events(e => e.Change("productnameprojectChange"))
.HtmlAttributes(new { style = "width: 70%;" })
.DataSource(ds =>
{
ds.Read("_PopulateProductNamesDD", "Common");
})
)
<label class="site-label" for="site">Project:</label>
@(Html.Kendo().DropDownList()
.Name("project")
.OptionLabel("All")
.DataTextField("ProjectName")
.DataValueField("ProjectId")
.AutoBind(false)
.Events(e => e.Change("projectChange"))
.HtmlAttributes(new { style = "width: 70%;" })
.DataSource(ds =>
{
ds.Read("_PopulateProjectsDD", "Common");
})
)
</div>
</text>);
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events
.Error("error_handler")
.Change("onChange")
)
.Model(model =>
{
model.Id(u => u.InventoryId);
model.Field(u => u.Size == u.SizeList.Size);
model.Field(u => u.WorkTypesMS).DefaultValue(new List<FS2.ViewModels.Common.InventoryWorkTypesMSViewModel>());
model.Field(u => u.SizeList).DefaultValue(
ViewData["defaultNewSize"] as FS2.ViewModels.Common.MeterSizeDDViewModel);
})
.PageSize(20)
.Read(read => read.Action("_InventoryListGrid", "Inventory").Data("onRead"))
.Create(create => create.Action("_InventoryListGridCreate", "Inventory").Data("serialize"))
.Update(update => update.Action("_InventoryListGridEdit", "Inventory").Data("serialize"))
.Destroy(destroy => destroy.Action("_InventoryListGridDestroy", "Inventory"))
)
)