0
votes

Using ASP.NET MVC action method to return Json to a view in order to display the data in a EXT JS form. I am always getting the "record" value as null in the function everythingLoaded even though i can see the action method is returning the JSON data after the Ext.Create. What i am trying to achieve is populate the firstName and lastName param's passed back from the action method to the textbox inside the panel. Can you help? Below is the code

->App.JS

Ext.onReady(function() {

  //Define Store  
    Ext.define('StudentModel', {
        extend: 'Ext.data.Model',
        idProperty: 'Id',
        fields: [
            { name: 'Id', type: 'int' },
            { name: 'firstName', type: 'string' },
            { name: 'lastName', type: 'string' },
            { name: 'birthDate', type: 'date' },
            { name: 'street', type: 'string' },
            { name: 'apartment', type: 'string' },
            { name: 'city', type: 'string' },
            { name: 'state', type: 'string' },
        ],
        validations: [{
            type: 'presence',
            field: 'firstName'
        }]
    });

var store = Ext.create('Ext.data.Store', {
    model: 'StudentModel',
    storeId: 'StudentStoreId',
    proxy: {
        type: 'ajax',
        url: '/Home/GetStudentSubmissions',
        reader: {
            type: 'json',
            rootProperty: 'data'
        }
    },
    autoLoad: { callback: everythingLoaded } 
 }
);

function everythingLoaded()
{
    var record = store.getAt(0);                
     //Here record is always null
    Ext.form('MyPanel').getForm().loadRecord(record); 
}

Ext.define('StudentView', {
    extend: 'Ext.form.Panel',
    id: 'MyPanel',
    alias: 'widget.StudentView',
    title: 'Student Information',
    resizable: false,
    collapsible: true,
    store:store,
    bodyPadding: '5',
    buttonAlign: 'center',
    border: false,
    trackResetOnLoad: true,
    layout: {
        type: 'vbox'
    },
    defaults: {
        xtype: 'textfield',
        msgTarget: 'side',
        labelAlign: 'top',
        labelStyle: 'font-weight:bold'
    },
    items: [{
        xtype: 'fieldcontainer',
        layout: 'hbox',
        defaultType: 'textfield',
        width: '100%',
        fieldDefaults: {
            labelAlign: 'top',
            labelStyle: 'font-weight:bold'
        },
    items: [{
        fieldLabel: 'First Name',
        name: 'firstName',
        allowBlank: false
    }, {
        fieldLabel: 'Last Name',
        name: 'lastName',
        allowBlank: false
        }]
    }]        
});

 //Here count is always 0
var i = store.getCount();

Ext.create('widget.StudentView', {
    renderTo: 'studentMasterForm'
});

});

-> C# Action Method

  public JsonResult GetStudentSubmissions()
    {
        return Json(GetStudents(), JsonRequestBehavior.AllowGet);
    }
    public Student GetStudents()
    {
        Student studobj = new Student();

        studobj.Id = 1;
        studobj.firstName = "Aravind";
        studobj.lastName = "R";
        studobj.state = "NJ";
        studobj.street = "Center Grove";
        studobj.birthDate = new DateTime(1989, 6, 6);
        studobj.apartment = "DD8";
        studobj.city = "XYZ";

        return studobj;

    }
}

->Student Model Class

public class Student { public int Id { get; set; } public string firstName { get; set; } public string lastName { get; set; } public DateTime birthDate { get; set; } public string street { get; set; } public string apartment { get; set; } public string city { get; set; } public string state { get; set; } }

1

1 Answers

0
votes

Your c# code returns one object "Student", but it is supposed to return an object which have an array of students as its "data" property.

You need to return an object of this type:

public class ExtStore
{
    public List<Student> data = new List<Student>();
}

this way for example:

public ExtStore GetStudents()
{
    Student studobj = new Student();

    studobj.Id = 1;
    studobj.firstName = "Aravind";
    studobj.lastName = "R";
    studobj.state = "NJ";
    studobj.street = "Center Grove";
    studobj.birthDate = new DateTime(1989, 6, 6);
    studobj.apartment = "DD8";
    studobj.city = "XYZ";

    ExtStore exs = new ExtStore();
    exs.data.Add(studobj);

    return exs;

}