Insertion of a new item in grid fails due to client template existence - kendo-ui

I have a Kendo UI grid which I need to be editable. I also need a client template for one of the columns.
The part from the kendo ui grid that bugs me:
.Columns(columns =>
{
columns.Bound(p => p.Author).Filterable(false).Width(100);
columns.Bound(p => p.Name).Filterable(false).Title("Idea Name");
columns.Bound(p => p.Description).Filterable(false);
columns.Bound(p => p.BeginDate).Format("{0:d}");
columns.Bound(p => p.ReleaseDate).Format("{0:d}");
columns.Bound(p => p.NextAvailableStates).ClientTemplate
(
"#for(var i = 0; i < NextAvailableStates.length; i++) {" +
"# <li>" +
" #=NextAvailableStates[i].StepName # " +
"</li> #" +
"} #"
)
.Title("Actions").IncludeInMenu(false).Visible(true)
.Sortable(false).Filterable(false);
})
My controller:
public partial class IdeaController : Controller
{
private StateMachineHelper helper = new StateMachineHelper();
public ActionResult Index(int? state)
{ //must ad to Model the next available states
var model = new List<Idea>();
model.AddRange(helper.GetIdeasByState(helper.GetStateByID(state)));
foreach (var item in model)
{
item.NextAvailableStates = helper.GetNextStates(helper.GetStateMachineInstancesByOrderID(item.ID));
}
return View(model);
}
public ActionResult NextState()
{
return View();
}
public ActionResult Read([DataSourceRequest]DataSourceRequest request, DateTime start, DateTime end)
{
var ideas = helper.GetIdeasBetweenDates(start, end);
DataSourceResult result = ideas.ToDataSourceResult(request);
return Json(result);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([DataSourceRequest] DataSourceRequest request, Idea product)
{
if (product != null && ModelState.IsValid)
{
var helper = new StateMachineHelper();
var machine = new StateMachineInstance() {
ParentID = 0,
//1 -> machineID
CurrentStateID = helper.GetFirstStateOfMachine(1).ID,
MachineID =1,
ParentStateID =0,
//OrderType: idea/command
OrderType = "Idea"
};
helper.AddNewOrder(product, machine);
product.NextAvailableStates = helper.GetNextStates(machine);
}
return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}
}
public Idea()
{
this.StateMachineInstances = new HashSet<StateMachineInstance>();
}
[ScaffoldColumn(false)]
[DataMember]
public int ID { get; set; }
[Required]
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
[Required]
public string Author { get; set; }
[DataMember]
public System.DateTime BeginDate { get; set; }
[DataMember]
public Nullable<System.DateTime> ReleaseDate { get; set; }
[DataMember]
public List<State> NextAvailableStates
{
get
{
return nextAvailableStates;
}
set
{
nextAvailableStates = value;
}
}
private List<State> nextAvailableStates = new List<State>();
public virtual ICollection<StateMachineInstance> StateMachineInstances { get; set; }
}
The grid renders just fine, with every column filled with the correct information. The problem appears when I need to add a new item to the grid. The popup window doesn't show up and I get Uncaught ReferenceError: NextAvailableStates is not defined. Which actually makes sense because the grid tries to draw before the new item gets returned.
My question: is there a way to achieve the insertion of a new item while using custom client templates?

You can try modifying your client template to check for the existence of NextAvailableStates before using them:
"# if (NextAvailableStates) {" +
" for(var i = 0; i < NextAvailableStates.length; i++) {" +
"# <li>" +
" #=NextAvailableStates[i].StepName # " +
"</li> #" +
" } " +
"} #"

Related

PetaPoco returns empty object

Inside my applocation, the petapoco poco returns an empty object (all values are null). Using the UI-O-Matic Nuget package inside my Umbraco 7.5.12.
The query i'm currently running:
var dbContext = ApplicationContext.Current.DatabaseContext;
var objects = dbContext.Database.Fetch<ObjectDB>("select Id, Name, CreatedOn, PlaceId, InActive, CityMapping, CountryIsoMapping, Globalsearch from ObjectsDB");
return objects.Where(n => n.PlaceId == PlaceId).FirstOrDefault();
TableDB is my PetaPoco model with the fields like:
[UIOMatic("ObjectsDB", "Object", "Object", FolderIcon = "icon-globe-inverted-europe-africa", ItemIcon = "icon-pin-location", RenderType = UIOMaticRenderType.List)]
[TableName("ObjectsDB")]
[PrimaryKey("Id", autoIncrement = false)]
[ExplicitColumns]
public class ObjectDB
{
[PrimaryKeyColumn(AutoIncrement = true)]
public int Id { get; set; }
[UIOMaticListViewFilter]
[UIOMaticListViewField(Name = "Name")]
[UIOMaticField(Name = "Name", Description = "Name")]
public string Name { get; set; }
}
When debuging:
`Debug result: con.Single<ObjectsDB>("select Name, Id from ObjectsDB where Id = 4")
This retruns the object:
{Umbraco.Extensions.Models.Custom.ObjectsModel.ObjectsDB} _createdOn: {1/1/0001 12:00:00 AM}
CityMapping: null
CountryIsoMapping: null
CreatedOn: {5/19/2017 4:22:16 PM}
Globalsearch: false
Id: 0
InActive: false
InCache: false
Name: null
Object: null
PlaceId: null `
Inserting data is working with the same dbContext, that's working.
What am I missing here?
I have used Petapoco in various Umbraco project and my approach is a bit different than your approach. I am sharing it here, hope it helps you.
This is the nuget package that I have used:(http://nuget.org/List/Packages/PetaPoco)
Please see my sample code below or in my blog:
[PetaPoco.TableName("fsCarts")]
[PetaPoco.PrimaryKey("RecordID")]
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public Guid ProductId { get; set; }
public int Count { get; set; }
public DateTime DateCreated { get; set; }
}
UmbracoDatabase con = ApplicationContext.Current.DatabaseContext.Database;
public void AddToCart(Product product)
{
try
{
var cartItem = con.FirstOrDefault<Cart>("SELECT * FROM fsCarts WHERE CartID=#0 AND ProductID=#1", ShoppingCardId, product.ProductId);
if (cartItem == null)
{
cartItem = new Cart
{
ProductId = product.ProductId,
CartId = ShoppingCardId,
Count = 1,
DateCreated = DateTime.Now
};
con.Insert("fsCarts", "RecordID", cartItem);
}
else
{
cartItem.Count++;
con.Update("fsCarts", "RecordID", cartItem);
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart AddToCart: " + ex.ToString())));
}
}
////////////////////
public int RemoveFromCart(int id)
{
int itemCount = 0;
try
{
var cartItem = con.FirstOrDefault<Cart>("SELECT * FROM fsCarts WHERE CartID=#0 AND RecordId=#1", ShoppingCardId, id);
if (cartItem != null)
{
if (cartItem.Count > 1)
{
cartItem.Count--;
itemCount = cartItem.Count;
con.Update("fsCarts", "RecordID", cartItem);
}
else
{
con.Delete("fsCarts", "RecordID", cartItem);
}
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart RemoveFromCart: " + ex.ToString())));
}
return itemCount;
}
////////////////////
public List<Cart> GetCartItems()
{
List<Cart> cartItemList = new List<Cart>();
try
{
cartItemList = con.Query<Cart>("SELECT * FROM fsCarts WHERE CartID=#0", ShoppingCardId).ToList();
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart GetCartItems: " + ex.ToString())));
}
return cartItemList;
}
////////////////////
public decimal GetTotal()
{
decimal? total = null;
try
{
total = con.ExecuteScalar<decimal>("SELECT SUM(ISNULL(p.Price,0)*c.Count) FROM fsCarts c INNER JOIN fsProducts p ON c.ProductID=p.ProductID WHERE c.CartID=#0", ShoppingCardId);
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart GetTotal: " + ex.ToString())));
}
return total ?? decimal.Zero;
}
Removing the attribute [ExplicitColumns] above my class fixed the problem. No everything works as expected. Also the other decorations are working. So #Nurhak Kaya was partially right. After removing that attribute deleting the table and rebuild / generating the table.

Summing in multi-level relationship

Using EF code first, I have the following 4 entities
public class Item {
public int Id { get; set; }
public string Name { get; set; }
}
public class Location {
public int Id { get; set; }
public string Name { get; set; }
}
public class InventoryAdjustment {
public int Id { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<AdjustmentLine> Lines { get; set; }
}
public class AdjustmentLine {
public int Id { get; set; }
public virtual Item Item { get; set; }
public int Quantity { get; set; }
}
What I am trying to do is to get the sum of all inventory adjustments for each item at each location using minimal database round-trips.
The best I achieved so far is:
using (var db = new InventoryContext()) {
var items = db.Items.ToList();
var locations = db.Locations.ToList();
foreach (var item in items) {
Console.WriteLine(item.Name+":");
foreach (var location in locations) {
Console.Write("\t" + location.Name + ": ");
var qty = db.InventoryAdjustments
.Where(p => p.Location.Id == location.Id)
.SelectMany(p => p.Lines)
.Where(p => p.Item.Id == item.Id)
.Select(p => (int?)p.Quantity)
.Sum();
Console.WriteLine(qty ?? 0);
}
}
Console.Read();
}
The above outputs:
Item1:
Location1: 2
Location2: 12
Location3: 21
Item2:
Location1: 4
Location2: 0
Location3: 0
Item3:
Location1: 1
Location2: 17
Location3: 0
But with 3 items and 3 locations in the database, the above code causes 11 calls to the database. 2 for getting items and locations, and 9 for calculating the sum of quantity.
Is there a better way to get the sum with the least amount of round-trips?
This should probably work:
using (var db = new InventoryContext())
{
var items = db.Items.ToList();
var locations = db.Locations.ToList();
items
.Select(item =>
{
Console.WriteLine(item.Name + ":");
return item;
})
.SelectMany(item => locations.Select(location => new { item, location }))
.GroupJoin(
db.InventoryAdjustments
.SelectMany(
inventoryAdjustment => inventoryAdjustment.Lines.Select(
adjustmentLine => new { key = new { locationId = inventoryAdjustment.Location.Id, itemId = adjustmentLine.Item.Id }, adjustmentLine.Quantity }
)
),
x => new { locationId = x.location.Id, itemId = x.item.Id },
y => y.key,
(x, y) =>
{
Console.WriteLine("\t {0}: {1}", x.location.Name, y.Sum(a => a.Quantity));
return 0;
}
).ToList();
Console.Write("\nPress any key...");
Console.ReadKey();
}

Model binding issues with Kendo complex object

My problem is very similar to this Model binding issues with Kendo objects with complex child properties . The only difference is that i have another level in the object.
My model is:
Public Person
{
public int Id {get;set;}
public string Name {get;set;}
public IEnumerable<Course> Courses {get;set;}
}
public Course
{
public int Id {get;set;}
public string Description {get;set;}
public IEnumerable<Schedule> Schedules {get;set;}
}
Public Schedule
{
public DateTime Init {get;set;}
public DateTime End {get;set;}
}
This model is bound to a KendoGrid. Everything works well, except that Init and End properties are always null when I posted the model.
In the Ajax Datasource :
.Update(update => update.Action("Update", "Controller").Data("serialize"))
.Create(create => create.Action("Create", "Controller").Data("serialize"))
<script>
function serialize(data) {
for (var property in data) {
if ($.isArray(data[property])) {
serializeArray(property, data[property], data);
}
}
};
function serializeArray(prefix, array, result) {
for (var i = 0; i < array.length; i++) {
if ($.isPlainObject(array[i])) {
for (var property in array[i]) {
result[prefix + "[" + i + "]." + property] = array[i][property];
}
}
else {
result[prefix + "[" + i + "]"] = array[i];
}
}
}
</script>
What I have to do to send the properties of the lists schedules?
I had also looked at their serializeArray solution, but it didn't work for me in case of 3 level objects I had. I could have fixed that but then I didn't want to write recursive code. The solution I used is pretty straight-forward and aligned to the problem I had. Its very readable.
I absolutely wish Kendo should do this out of the box for their grid, but they told this when I raised a support question.
"You will need to send the values as additional data in this case because the built-in filtering does not support collection values. To format the data so that it will be bound by the model binder, you should follow the guidelines from my previous reply(dot notation for objects and indexer for arrays)"
Here is my C# ViewModels
//relates to one control value (for e.g. one entry in multi-select)
public class FormUnitFilter
{
public string Operator { get; set; }
public string Field { get; set; }
public string Value { get; set; }
public List<string> ValueList { get; set; }
}
//relates to a set of filters in a combined set (for e.g. the whole multi-select or a radiobutton or date control which appears in a single panel)
public class FormSetFilter
{
public List<FormUnitFilter> Filters { get; set; }
public string LogicalOperator { get; set; }
}
//relates to the whole set of filters present on the screen (for e.g. the filters across different panels)
public class FormWholeFilter
{
public List<FormSetFilter> Filters { get; set; }
public string LogicalOperator { get; set; }
}
here is my js function which converts this json model to a type recognized by MVC controller action parameter.
function buildFilterCriteria() {
var data = {};
if (modelObj) {
//reset the filters
modelObj.FormWholeFilter.Filters.length = 0;
//Assign FormWholeFilter data (outermost object)
data["FormWholeFilter.LogicalOperator"] = modelObj.FormWholeFilter.LogicalOperator;
//now iterate the filters inside FormWholeFilter (1st inner object)
for (var setIndex = 0; setIndex < modelObj.FormWholeFilter.Filters.length; setIndex++) {
var setFilter = modelObj.FormWholeFilter.Filters[setIndex];
data["FormWholeFilter.Filters[" + setIndex + "].LogicalOperator"] = setFilter.LogicalOperator;
//now iterate the filters inside FormSetFilter (2nd inner object)
for (var unitIndex = 0; unitIndex < setFilter.Filters.length; unitIndex++) {
var unitFilter = setFilter.Filters[unitIndex];
data["FormWholeFilter.Filters[" + setIndex + "].Filters[" + unitIndex + "].Operator"] = unitFilter.Operator;
data["FormWholeFilter.Filters[" + setIndex + "].Filters[" + unitIndex + "].Field"] = unitFilter.Field;
data["FormWholeFilter.Filters[" + setIndex + "].Filters[" + unitIndex + "].Value"] = unitFilter.Value;
if (unitFilter.ValueList)
for (var valIndex = 0; valIndex < unitFilter.ValueList.length; valIndex++) {
data["FormWholeFilter.Filters[" + setIndex + "].Filters[" + unitIndex + "].ValueList[" + valIndex + "]"] = unitFilter.ValueList[valIndex];
}
}
}
}
return modelObj && data;
}
Here is my controller action method which takes the Kendo grid datasourcerequest and the FormWholeFilter I pass from JavaScript.
public JsonResult ProcessFilters([DataSourceRequest] DataSourceRequest request, FormWholeFilter formWholeFilter)
{
//Method body
}
Also, when I load the page for the first time, I had assigned the modelObj to the FormWholeFilter blank json like this and thats why I could use this variable in the buildFilterCriteria method:
var modelObj;
$(document).ready(function () {
modelObj = $.parseJSON('#Html.Raw(Json.Encode(#Model))');
});

Array of checkboxes in form

I have a viewmodel to use in the Create view:
ViewModel
public class ReportViewModel
{
public int ID { get; set; }
[Display(Name = "Platform")]
public string Platform { get; set; }
[Display(Name = "Logo")]
public HttpPostedFileBase Logo { get; set; }
}
Create View
#model HPRWT.ViewModels.ReportViewModel
#using (Html.BeginForm("Create", "Report", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
#Html.LabelFor(model => model.Platform )
#Html.EditorFor(model => model.Platform )
#Html.ValidationMessageFor(model => model.Platform)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Logo)
<input type="file" id="Logo" name="Logo" />
</div>
}
This work perfect. But now I need an array of checkboxes (7x24) to get free hours (7 day, 24 hours). I have an array of ids (i need a defined id because I use jquery). In Create view:
#for(int i = 1; i < labels.Length; i++)
{
<tr>
<td>#labels[i][0]</td>#for(int j = 1; j < labels[i].Length; j++)
{
<td><div><input type="checkbox" id="#ids[i][j]" /><label for="#ids[i][j]"></label></div></td>
}
My ids are like R02C00 (R of row + numer of row with 2 digits + C (column) + number of column (2 digits). I generate them with:
for (int i = 1; i < 8; i++)
for (int j = 1; j < 25; j++)
ids[i][j] = "R" + i.ToString("00") + "C" + (j-1).ToString("00");
This also works well. Now my problem is how I get the checkboxes values.
Controller
[HttpPost]
public ActionResult Create(ReportViewModel rvm)
{
if (ModelState.IsValid)
{
rdb.Reports.Add(CreateReport(rvm));
rdb.SaveChanges();
return RedirectToAction("Index");
}
return View(rvm);
}
// Create a report from a reportviewmodel
private Report CreateReport(ReportViewModel rvm)
{
Report report = new Report();
// Platform
string platform = rvm.Platform;
report.Platform = platform ;
// Logo
HttpPostedFileBase inputFile = rvm.InputFile; // Some code to get the path
return report;
}
How can I get the checkboxes values? If I add in reportviewmodel a bool[][], is there is any way to do a #Html.Checkbox? (If I have to change ids names in jquery, I donĀ“t mind, is not mandatory to have a id like R01C01... only the ids in jquery be the same as html)
Why don't you use a view model?
public class FreeHourViewModel
{
public string Label { get; set; }
public bool Selected { get; set; }
}
public class ReportViewModel
{
public ReportViewModel()
{
this.FreeHours = Enumerable
.Range(1, 7)
.Select(day =>
Enumerable.Range(1, 24).Select(hour => new FreeHourViewModel
{
Label = string.Format("day: {0}, hour: {1}", day, hour)
}
).ToArray()
).ToArray();
}
public int ID { get; set; }
[Display(Name = "Platform")]
public string Platform { get; set; }
[Display(Name = "Logo")]
public HttpPostedFileBase Logo { get; set; }
public FreeHourViewModel[][] FreeHours { get; set; }
}
and then:
#for (int i = 0; i < Model.FreeHours.Length; i++)
{
for (int j = 0; j < Model.FreeHours[i].Length; j++)
{
#Html.LabelFor(x => x.FreeHours[i][j].Selected, Model.FreeHours[i][j].Label)
#Html.CheckBoxFor(x => x.FreeHours[i][j].Selected)
}
}
And when the form is submitted, since you have used a real view model, model binding will work as expected. Also you don't need any jQuery to generate those checkboxes. Strongly typed helpers such as Html.CheckBoxFor is the way to go.
How about having a strongly-typed view in relation to your checkboxes?
public class ReportViewModel
{
public ReportViewModel()
{
Days = new List<Day>();
}
public int ID { get; set; }
[Display(Name = "Platform")]
public string Platform { get; set; }
[Display(Name = "Logo")]
public HttpPostedFileBase Logo { get; set; }
public IList<Day> Days { get; set; }
}
public class Hour
{
public int Id { get; set; }
public bool Selected { get; set; }
}
public class Day
{
public Day()
{
Hours = new List<Hour>();
}
public int Id { get; set; }
public bool Selected { get; set; }
public IList<Hour> Hours { get; set; }
}
You then return a view like this:
foreach (var d in Enum.GetValues(typeof(DayOfWeek)))
{
var day = new Day { Id = (int)d };
for (var i = 0; i < 25; i++)
{
day.Hours.Add(new Hour { Id = i });
}
model.Days.Add(day);
}
return View(model);
And add this to your view:
for (var i = 0; i < 7; i++)
{
<div id="days">
<ul>
#for (var j = 0; j < 24; j++)
{
<li>#Html.CheckBoxFor(m=>Model.Days[i].Hours[j].Selected)</li>
}
</ul>
</div>
}
Now when you receive your input you have an array of bool values where you have a sort of Ids, that is 0-6 for days and 0-23 for hours. You can easily determine what hour in what particular day was selected.
Of course, you have to fill in the missing pieces with my suggestion like showing the labels for each checkboxes.

Telerik MVC Grid with Dynamic Columns at Run Time from a Collection or Dictionary

After spending the last couple days searching, I'm officially stuck. I'm working on a binding an object to the Telerik MVC 3 Grid, but the catch is that it needs to have dynamically created columns (not auto generated). Three of the columns are known, the others are unknown, and this is the tricky part. Basically, it can be like these examples:
KnownColumn1 | KnownColumn2 | UnknownColumn1 | KnownColumn3
KnownColumn1 | KnownColumn2 | UnknownColumn1 | UnknownColumn2 | UnknownColumn3 | KnownColumn3
etc.
Because I'm putting the unknown columns in a list (I've tried a dictionary too so I can get the column names), this has complicated things for me when binding. My code is below:
Model (There can be zero to hundreds of rows, but this model is in a view model of type List, there can also be 0 to 20 plus columns that are dynamically added)
public class VendorPaymentsGLAccount
{
public string GeneralLedgerAccountNumber { get; set; }
public string GeneralLedgerAccountName { get; set; }
public string DisplayName { get { return string.Format("{0} - {1}", GeneralLedgerAccountNumber, GeneralLedgerAccountName); } }
public Dictionary<string, double> MonthAmount { get; set; }
public double Total { get { return MonthAmount.Sum(x => x.Value); } }
public List<string> Columns { get; set; }
public List<double> Amounts { get; set; }
public VendorPaymentsGLAccount() { }
}
View (The section that's commented out was trying to use the dictionary)
<fieldset>
<legend>General Ledger Account Spend History</legend>
#if (Model.VendorPaymentsGLAccounts != null)
{
<br />
#(Html.Telerik().Grid(Model.VendorPaymentsGLAccounts)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(gl => gl.DisplayName).Title("General Ledger Account").Width(200).Filterable(false).Sortable(false);
//foreach (var month in Model.VendorPaymentsGLAccounts[0].MonthAmount)
//{
// //columns.Bound(gl => gl.MonthAmount[month.Key.ToString()].ToString()).Title(month.Key.ToString()).Width(100).Filterable(false).Sortable(false);
// //columns.Template(v => Html.ActionLink(v.VoucherID, "VoucherSummary", new { id = v.VoucherID, bu = v.BusinessUnitID, dtt = v.InvoiceDate.Ticks })).Title("Voucher").Width(100);
// columns.Template(gl => Html.ActionLink(gl.MonthAmount[month.Key.ToString()].ToString(), "VoucherSummary")).Title(month.Key.ToString()).Width(100);
//}
for (int i = 1; i <= (Model.VendorPaymentsGLAccounts[0].Columns.Count() - 1); i++)
{
string colTemp = Model.VendorPaymentsGLAccounts[0].Columns[i - 1];
columns.Template(gl => gl.Amounts[i - 1]).Title(colTemp).Width(100);
}
columns.Template(gl => String.Format("{0:C}", gl.Total)).Title("Total");
})
.Sortable()
.Pageable()
.Filterable()
.Footer(true))
}
else
{
<br />
#:There are no records that match your selected criteria.
}
</fieldset>
Using the dictionary approach, I was able to get the columns generated correctly with the right header text, but the values for the columns (in my testing there were only 2 columns) were the same. Can anyone help with this? This seems to be an oddball issue. Just trying to figure out how to do this correctly.
Update: Here is a screen shot using the dictionary approach that shows the issue. The column headings are correct, but the values are the same for both of the dynamic columns.
Using dynamically defined columns with the Telerik grid control can be tricky. But in your case, it's mainly a typical pitfall of closures.
In the following loop, the compiler will bind each instance of gl => gl.Amounts[i - 1] to the variable i and evaluate it later:
for (int i = 1; i <= (Model.VendorPaymentsGLAccounts[0].Columns.Count() - 1); i++)
{
string colTemp = Model.VendorPaymentsGLAccounts[0].Columns[i - 1];
columns.Template(gl => gl.Amounts[i - 1]).Title(colTemp).Width(100);
}
In fact, it's evaluated after the loop has finished. So i will always have the value that lead to the completion of the loop.
The fix is to use a temporary variable:
for (int i = 1; i <= (Model.VendorPaymentsGLAccounts[0].Columns.Count() - 1); i++)
{
string colTemp = Model.VendorPaymentsGLAccounts[0].Columns[i - 1];
int columnIndex = i - 1;
columns.Template(gl => gl.Amounts[columnIndex]).Title(colTemp).Width(100);
}
I had the same problem, and was googling plenty of hours around, and have done a lot of attempts from various assistance lines. But even so, it was not so trivial to solve!
And for that reason and to have also another working example here, I will provide also my solution!
Information: It only works at my place with an IList Model. Other collections had also caused problems!
#model IList<CBS.Web.Models.Equipment.EquipmentViewModel>
#(Html.Telerik().Grid(Model)
.Name("Grid")
.DataKeys(keys =>
{
keys.Add(m => m.ID);
})
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
// renders the grid initially
.Select("EquipmentGrid", "Equipment");
})
.Columns(columns =>
{
// Equipment IDs
columns.Bound(m => m.ID).Hidden(true);
columns.Bound(m => m.Name).Title("Equipments").Width(200);
// Every item (EquipmentViewModel) of the Model has the same Count of Fields
for (int i = 0; i < (Model[0].Fields.Count()); i++)
{
// Name of the column is everytime same as in Model[0]
string columnName = Model[0].Fields.ElementAt(i).FieldDefinition.Name;
// Constructs i-counted columns, dynamically on how much
// Fields are owned by an Equipment. But note, that all Equipment-items
// in the Model must have the same Count and disposal of Fields!
columns.Template(m => m.Fields
.Where(f => f.FieldDefinition.Name == columnName)
.Where(f => f.EquipmentId == m.ID).First().Value)
.Title(columnName)
.Width(columnName.Length * 8); // * 8 was the optimal lenght per character
}
})
.ClientEvents(events => events.OnRowSelect("onRowSelected"))
.Selectable()
.Resizable(resizing => resizing.Columns(true))
.Pageable()
.Scrollable()
.Groupable()
.Filterable()
)
Controller:
public ActionResult EquipmentGrid(Guid id)
{
var belongingEquipments = _equipmentRepository.GetNotDeleted()
.OrderBy(e => e.Name).ToList()
.Where(e => e.RevisionId == id);
List<EquipmentViewModel> equVMList = new List<EquipmentViewModel>();
for (int i = 0; i < belongingEquipments.Count(); i++)
{
var equVM = new EquipmentViewModel
{
ID = belongingEquipments.ElementAt(i).ID,
Name = belongingEquipments.ElementAt(i).Name,
RevisionId = belongingEquipments.ElementAt(i).RevisionId,
EquipmentTypeId = belongingEquipments.ElementAt(i).EquipmentTypeId,
Fields = SortFields(belongingEquipments.ElementAt(i).Fields.ToList())
};
equVMList.Add(equVM);
}
return PartialView("EquipmentGrid", equVMList);
}
Models:
namespace CBS.Web.Models.Equipment
{
public class EquipmentViewModel
{
public Guid ID { get; set; }
public string Name { get; set; }
public Guid RevisionId { get; set; }
public Guid EquipmentTypeId { get; set; }
public virtual ICollection<FieldEntity> Fields { get; set; }
}
}
FieldDefinition
namespace CBS.DataAccess.Entities
{
public class FieldDefinitionEntity : EntityBase
{
[Required]
public virtual Guid EquipmentTypeId { get; set; }
public virtual EquipmentTypeEntity EquipmentType { get; set; }
[Required(AllowEmptyStrings = false)]
public virtual string Name { get; set; }
public virtual int Numbering { get; set; }
[Required]
public virtual Guid TypeInformationId { get; set; }
public virtual TypeInformationEntity TypeInformation { get; set; }
public virtual ICollection<FieldEntity> Fields { get; set; }
}
}
Field
namespace CBS.DataAccess.Entities
{
public class FieldEntity : EntityBase
{
[Required]
public virtual Guid EquipmentId { get; set; }
public virtual EquipmentEntity Equipment { get; set; }
[Required]
public virtual Guid FieldDefinitionId { get; set; }
public virtual FieldDefinitionEntity FieldDefinition { get; set; }
public virtual string Value { get; set; }
}
}
I dynamically bind the columns at runtime with reflection:
#model IEnumerable<object>
#using System.Collections
#using System.Collections.Generic
#using System.Reflection;
#(Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
Type t = Model.GetType().GetGenericArguments()[0];
foreach (var prop in t.GetProperties())
{
if (IsCoreType(prop.PropertyType))
{
columns.Bound(prop.PropertyType, prop.Name);
}
}
})
.DataBinding(binding => binding.Ajax()
.Select("SelectMethod", "SomeController")
)
.Sortable()
.Pageable()
.Filterable()
.Groupable()
)
#functions{
public bool IsCoreType(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsCoreType(type.GetGenericArguments()[0]);
}
return !(type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
}
}
Here is the workaround:
#(Html.Telerik().Grid(Model.Users)
.Name("Grid")
.Columns(columns => {
columns.GenerateCustomColumns(columnSettings);
}).DataBinding(dataBinding => dataBinding.Ajax().Select("_getusers", "home"))
.Scrollable(scrolling => scrolling.Enabled(true).Height("auto"))
.Pageable(paging => paging.Enabled(true)
.PageSize(10, new int[] { 5, 10, 20, 50, 100, 500 })
.Position(GridPagerPosition.Both)
.Total(Model.Users.Count)
.Style(GridPagerStyles.PageSizeDropDown | GridPagerStyles.NextPreviousAndNumeric)
.PageTo(1))
.Filterable(filtering => filtering.Enabled(true))
.Reorderable(reordering => reordering.Columns(true))
.NoRecordsTemplate(" ")
.EnableCustomBinding(true)
)
// Extension method to genarate columns dynamically
public static class TelerikMvcGridColumnHelper
{
public static void GenerateCustomColumns<T>(this GridColumnFactory<T> columns,List<GridCustomColumnSettings> settings) where T:class
{
if (settings != null)
{
settings.ForEach(column =>
{
var boundedColumn = columns.Bound(column.Member);
if (column.ClientFooterTemplate != null)
boundedColumn.ClientFooterTemplate(column.ClientFooterTemplate);
if (!string.IsNullOrEmpty(column.Width))
boundedColumn.Width(column.Width);
});
}
}
}
// Column settings class
public class GridCustomColumnSettings : GridColumnSettings
{
public string ClientFooterTemplate { get; set; }
}
I did this simple way. NOTE : the following solution works in ajax edit mode too (not just a read-only grid) :
when the ViewModels are :
public class PriceSheetEditGridViewModel
{
public IEnumerable<PriceSheetRowViewModel> Rows { get; set; }
public IEnumerable<PriceSheetColumnViewModel> Columns { get; set; }
}
public class PriceSheetColumnViewModel
{
public int Id { get; set; }
public string Title { get; set; }
}
public class PriceSheetRowViewModel
{
public int RowNo { get; set; }
public string Description { get; set; }
public double?[] Prices { get; set; }
}
the view can be like this (part of view.cshtml file...) :
....
#model PriceSheetEditGridViewModel
...
columns.Bound(o => o.Description ).Width(150);
int i = 0;
foreach (var col in Model.Columns)
{
columns
.Bound(model => model.Prices).EditorTemplateName("PriceSheetCellPrice").EditorViewData(new { ColumnId = i })
.ClientTemplate("<span><#=Prices ? jsHelper.addCommas(Prices[" + i.ToString() + "]):null#></span>")
.Title(col.Title).Width(80);
i++;
}
....
and the PriceSheetCellPrice.cshtml editor template file (in shared\editortemplates folder) :
#model decimal?
#(Html.Telerik().NumericTextBox()
.Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)+"["+ViewBag.ColumnId+"]")
.InputHtmlAttributes(new { style = "width:100%" })
})
.EmptyMessage("")
.DecimalDigits(0)
.DecimalSeparator(",")
.MinValue(0)
.Value((double?) Model)
)

Resources