PDA

View Full Version : how to read data from ASP.NET MVC ViewData in ExtJS?



lszk
12 Aug 2009, 5:32 PM
Hi.

In action method I have

ViewData["genres"] = /*new SelectList(*/repository.GetGenres().ToArray();

How can I read this data using one of available readers in ExtJS? I want to populate combobox with this data...
In this ViewData is a json string.
I don't need to do request to some page to get the data, because this data is available from the current page where I have my form, but I don't know to get it using ExtJS. In HttpProxy is url: 'path/to/page'. Is this possible to substitute this area with ready object with data? Or maybe is available other config option where is holding the result after request? In this case I could set the data manually with my VieData object...

lszk
13 Aug 2009, 7:32 AM
After a few hours I have the solution.
Code from action method:

JavaScriptSerializer serializer = new JavaScriptSerializer();
ViewData["genres"] = serializer.Serialize(repository.GetGenres().ToList());

ExtJS code:


var genres = new Ext.data.Store({
reader: new Ext.data.JsonReader({
fields: ['GenreId', 'Name']
}),
proxy: new Ext.data.MemoryProxy(<%= ViewData["genres"] %>)
});
genres.load();

//...
xtype: 'combo',
mode: 'local',
displayField: 'Name',
fieldName: 'GenreId',
store: genres,
name: 'Genres',
fieldLabel: 'Genres',
width: 120