PDA

View Full Version : [CLOSED] Dynamic store fields being added more than once (MVC Razor)



JakeM
Jul 18, 2014, 2:28 AM
I am building a grid dynamically in the controller and placing it in a panel as the container with a direct event. When the store is being loaded on the client side I get the following error:
Unhandled exception at line 131, column 3953 in eval code
0x800a138f - JavaScript runtime error: Unable to get property 'addField' of undefined or null reference

a portion of the Call Stack follows:


Ext.getCmp("2container").add({store:{model:Ext.define(Ext.id(), {extend: "Ext.data.Model", fields:[{name:"Id",type:"int"},{name:"plusHolder",type:"string"},{name:"04/30/2014",type:"float"}
...
viewConfig:{xtype:"gridview"}});App.id41d1507a39dc6ac4.addField({"name":"04/30/2014","type":"float"},2,true);

I hope that you can see that the last bit there is a second attempt to load the same field.

If I continue to run through the error, my data shows correctly, but every time I access that grid I get the error.
Here is the code for the Panel:


X.Panel()
.Title("Allocations")
.ID(ViewBag.clientId + "container")
.Items(
X.Button()
.Text("Add Grid")
.Icon(Icon.Add)
.DirectEvents(de =>
{
de.Click.Url = Url.Action("CreateAllocationGrid");
de.Click.ExtraParams.Add(new { id = ViewBag.clientId, containerId = ViewBag.clientId + "container" });
de.Click.EventMask.ShowMask = true;
de.Click.EventMask.Target = MaskTarget.CustomTarget;
de.Click.EventMask.CustomTarget = ViewBag.ClientId + "container";
de.Click.Before = "this.hide()";
})


and the code for the Controller


public ActionResult CreateAllocationGrid(string id, string containerId)
{
int clientId = Convert.ToInt32(id);
Client client = ClientRepository.GetClientDetails(db, clientId);
List<DateTime> dateTimeList = new List<DateTime>();
foreach (DateTime clientValueDate in client.ClientValues.Keys.OrderByDescending(d => d.Date).Distinct())
{
string dateString = clientValueDate.ToString("d");
dateTimeList.Add(clientValueDate);
}

this.BuildGrid1(dateTimeList, client.Id).AddTo(containerId);
return this.Direct();
}
private Ext.Net.GridPanel BuildGrid1(List<DateTime> dateTimeList, int clientId)
{
Client client = ClientRepository.GetClientDetails(db, clientId);
Ext.Net.GridPanel grid = new Ext.Net.GridPanel
{
Border = false,
Store =
{
new Store
{
Proxy =
{
new AjaxProxy
{
Url = Url.Action("AllocationStore"),
Reader = { new JsonReader() { Root = "data" } }
}
},
Parameters =
{
new StoreParameter
{
Name = "id",
Value = clientId.ToString(),
Mode = ParameterMode.Raw
}
},
Model =
{
new Model
{
IDProperty = "Id",
Fields =
{
new ModelField("Id", ModelFieldType.Int),//0 Client ID
}
}
}
}
},
ColumnModel =
{
Columns =
{
new Column { Hidden = true, DataIndex = "Id", Locked = true } //0 Client ID
}
},
View =
{
new Ext.Net.GridView()
{
StripeRows = true,
TrackOver = true
}
}
};
Store store = grid.GetStore();
int fieldCount = 2;
foreach (DateTime dt in dateTimeList)
{
string dateString = dt.Date.ToString("d");
ModelField field = new ModelField(dateString, ModelFieldType.Float);//this is the value field for the date
store.AddField(field, fieldCount);//the field "date" and order that it appears in the store for the heading
fieldCount++;
ModelField offOnField = new ModelField(dateString + "OffOn", ModelFieldType.Boolean);
store.AddField(offOnField, fieldCount);
fieldCount++;

Column col = new Column
{
Renderer = { Fn = "checkFlag" }
};
col.Text = dateString;
col.DataIndex = dateString;
col.Renderer.Format = RendererFormat.UsMoney;
grid.ColumnModel.Columns.Add(col);
}
return grid;
}
public ActionResult AllocationStore(string id)
{
int clientId = Convert.ToInt32(id);
Client client = ClientRepository.GetClientDetails(db, clientId);
List<DateTime> dateTimeList = new List<DateTime>();
List<object> clientStuff = new List<object>();
var _tempObject = new ExpandoObject() as IDictionary<string, Object>;
_tempObject.Add("Id", client.Id);
string expand = "Expand";
_tempObject.Add("plusHolder", expand);
foreach (DateTime clientValueDate in client.ClientValues.Keys.OrderByDescending(d => d.Date).Distinct())
{
string dateString = clientValueDate.ToString("d");
dateTimeList.Add(clientValueDate);
_tempObject.Add(dateString, client.ClientValues[clientValueDate]);
if (client.ClientRecommendationOffOnDateClient.Contai nsKey(clientValueDate))
{
_tempObject.Add(dateString + "OffOn", client.ClientRecommendationOffOnDateClient[clientValueDate]);
}
else
{
_tempObject.Add(dateString + "OffOn", true);
}
}
clientStuff.Add(_tempObject);
return this.Store(clientStuff);
}

Any help would be greatly appreciated.

Jake

Daniil
Jul 18, 2014, 2:24 PM
Hi Jake,

The AddField should not be used in your scenario.

Please put all the ModelFields to a Store's Model, like this:


Store store = new Store();
Model model = new Model();

model.Fields.Add(new ModelField("name1"));
model.Fields.Add(new ModelField("name2"));

store.Model.Add(model);

As a side note I would avoid such Names for ModelFields:

"name":"04/30/2014"

Well, I am not 100% sure it might cause any problems, but I just intuitively feel it might.

JakeM
Jul 18, 2014, 3:39 PM
Thank you, Daniil, once more you have helped me tremendously. I should have thought to add the ModelFields to the Model then the Store. You can mark this thread complete.