在EXT窗体面板中填充EXT JS存储

Populating EXT JS Store in a EXT form Panel

本文关键字:EXT 填充 JS 存储 窗体 体面      更新时间:2023-09-26

使用ASP.NET MVC操作方法将Json返回到视图,以便以EXT JS形式显示数据。我总是在函数everythingLoaded中得到null的"record"值,尽管我可以看到操作方法在Ext.Create之后返回JSON数据。我试图实现的是填充从操作方法传递回面板内文本框的firstName和lastName参数。你能帮忙吗?以下是代码

->应用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#动作方法

  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;
    }
}

->学生模型类

公开课学生{公共int Id{get;set;}公共字符串firstName{get;set;}公共字符串lastName{get;set;}public DateTime出生日期{get;set;}公共字符串street{get;set;}公共字符串单元{get;set;}公共字符串city{get;set;}公共字符串状态{get;set;}}

您的c#代码返回一个对象"Student",但它应该返回一个以学生数组作为其"data"属性的对象。

您需要返回以下类型的对象:

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

例如:

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;
}