简体   繁体   English

在EXT表单面板中填充EXT JS商店

[英]Populating EXT JS Store in a EXT form Panel

Using ASP.NET MVC action method to return Json to a view in order to display the data in a EXT JS form. 使用ASP.NET MVC操作方法将Json返回到视图,以便以EXT JS形式显示数据。 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. 我始终在everythingLoaded函数中将“ record”值获取为null,即使我可以看到action方法在Ext.Create之后返回JSON数据。 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. 我试图实现的是将firstName和lastName参数从action方法传递回面板内的文本框。 Can you help? 你能帮我吗? Below is the code 下面是代码

->App.JS -> 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 -> 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;

    }
}

->Student Model Class ->学生模型课

public class Student { public int Id { get; 公共班级学生{公共int ID {获取; set; 组; } public string firstName { get; }公用字符串firstName {get; set; 组; } public string lastName { get; }公用字符串lastName {get; set; 组; } public DateTime birthDate { get; }公开的DateTime birthDate {get; set; 组; } public string street { get; }公共街区{ set; 组; } public string apartment { get; }公共字串公寓{get; set; 组; } public string city { get; }公共字符串城市{get; set; 组; } public string state { get; }公共字符串状态{get; set; 组; } } }}

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. 您的C#代码返回一个对象“ Student”,但应该返回一个对象,该对象具有一个学生数组作为其“数据”属性。

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM