MVC Ext.NET,如何从模型创建数据绑定

时间:2012-02-01 14:23:33

标签: c# asp.net-mvc extjs ext.net

我正在使用MVC3 + Ext.NET控制器

这是我的模特:

namespace MvcApplication32.Models
{
    public class Subsystem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string KB { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime{ get; set; }
        public DateTime BuildTime { get; set; }
        public bool Result{ get; set; }

        public Subsystem[] GetSubsystems()
        {
            return new Subsystem[]
                       {
                           new Subsystem() {Id=1, Name = "A", KB="KB112066", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                           new Subsystem() {Id=2, Name = "B", KB="KB143226", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=false},
                           new Subsystem() {Id=3, Name = "C", KB="KB115326", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                           new Subsystem() {Id=4, Name = "D", KB="KB121425", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                       };
        }

...

这是我的控制器(没什么特别的)

namespace MvcApplication32.Controllers
{
    public class Temp1Controller : Controller
    {
        //
        // GET: /Temp1/

        public ActionResult Index()
        {
            return View();
        }

这是我的观点:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    if (!X.IsAjaxRequest)
    {
        Subsystem s = new Subsystem();
        this.Store1.DataSource = s.GetSubsystems();
        this.Store1.DataBind();
    }
}
.
.

<script type="text/javascript">
    var template = '<span style="color:{0};">{1}</span>';

    var change = function (value) {
        return String.format(template, (value > 0) ? "green" : "red", value);
    };

    var pctChange = function (value) {
        return String.format(template, (value > 0) ? "green" : "red", value + "%");
    };
    </script>


    <ext:ResourceManager ID="ResourceManager1" runat="server" />

    <h1>Grid Panel --> Array Grid --> Simple Array Grid</h1>

    <ext:GridPanel 
        ID="GridPanel1"
        runat="server" 
        StripeRows="true"
        Title="Array Grid" 
        TrackMouseOver="true"
        Width="600" 
        Height="350"
        AutoExpandColumn="name">
        <Store>
            <ext:Store ID="Store1" runat="server">
                <Reader>
                    <ext:ArrayReader>
                        <Fields>
                            <ext:RecordField Name="id" Type=Int />
                            <ext:RecordField Name="name" Type=String/>
                            <ext:RecordField Name="kb" Type=String />
                            <ext:RecordField Name="startTime" Type='date' />
                            <ext:RecordField Name="endTime" Type=String DateFormat="yyyy-MM-dd" />
                            <ext:RecordField Name="buildTime" Type=Auto />
                            <ext:RecordField Name="status" Type=Boolean />
                        </Fields>
                    </ext:ArrayReader>
                </Reader>
            </ext:Store>
        </Store>
        <ColumnModel ID="ColumnModel1" runat="server">
            <Columns>
                <ext:Column ColumnID="Id" Header="Id" DataIndex="id" />
                <ext:Column ColumnID="Name" Header="Name" DataIndex="name" />
                <ext:Column ColumnID="KB" Header="KB" DataIndex="kb" />
                <ext:DateColumn ColumnID="StartTime" Header="StartTime" DataIndex="startTime" Format="HH:mm:ss" />
                <ext:DateColumn ColumnID="EndTime" Header="EndTime" DataIndex="endTime" />
                <ext:DateColumn ColumnID="BuildTime" Header="BuildTime" DataIndex="buildTime" />
                <ext:Column ColumnID="Status" Header="Status" DataIndex="status" />
            </Columns>
        </ColumnModel>
        <SelectionModel>
            <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="true" />
        </SelectionModel>
    </ext:GridPanel>
</body>

</asp:Content>

我遇到的问题是我无法将子系统类绑定到控制器中,只有当我将其创建为Object时它才起作用。 这对我不起作用:

public Subsystem[] GetSubsystems()
{
    return new Subsystem[]
               {
                   new Subsystem() {Id=1, Name = "A", KB="KB112066", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                   new Subsystem() {Id=2, Name = "B", KB="KB143226", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=false},
                   new Subsystem() {Id=3, Name = "C", KB="KB115326", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                   new Subsystem() {Id=4, Name = "D", KB="KB121425", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
               };
}

只有当我像这样创建它时,它才能正常工作:

public object[] GetSubsystems()
{
    return new object[]
               {
                   new object[] {Id=1, Name = "A", KB="KB112066", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                   new object[] {Id=2, Name = "B", KB="KB143226", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=false},
                   new object[] {Id=3, Name = "C", KB="KB115326", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
                   new object[] {Id=4, Name = "D", KB="KB121425", StartTime=DateTime.Now, EndTime=DateTime.Now.AddDays(1), BuildTime=DateTime.Now, Result=true},
               };
}

我不想创建我的所有类作为对象,我想创建各种类,如子系统,员工,人等,而不是能够将它们绑定到控制器 可以这样做吗?

1 个答案:

答案 0 :(得分:2)

您必须使用JsonReader并以正确的大小写字段名称。

Screenshot

为GridPanel尝试此代码:

<ext:GridPanel 
        ID="GridPanel1"
        runat="server" 
        StripeRows="true"
        Title="Array Grid" 
        TrackMouseOver="true"
        Width="600" 
        Height="350"
        AutoExpandColumn="Name">
        <Store>
            <ext:Store ID="Store1" runat="server">
                <Reader>
                    <ext:JsonReader >
                        <Fields>
                            <ext:RecordField Name="Id"  />
                            <ext:RecordField Name="Name" />
                            <ext:RecordField Name="KB"  />
                            <ext:RecordField Name="StartTime" Type="Date" />
                            <ext:RecordField Name="EndTime" Type="Date" />
                            <ext:RecordField Name="BuildTime" />
                            <ext:RecordField Name="Status" Type="Boolean" />
                        </Fields>
                    </ext:JsonReader>
                </Reader>
            </ext:Store>
        </Store>
        <ColumnModel ID="ColumnModel1" runat="server">
            <Columns>
                <ext:Column ColumnID="Id" Header="Id" DataIndex="Id" />
                <ext:Column ColumnID="Name" Header="Name" DataIndex="Name" />
                <ext:Column ColumnID="KB" Header="KB" DataIndex="KB" />
                <ext:DateColumn ColumnID="StartTime" Header="StartTime" DataIndex="StartTime" Format="HH:mm:ss" />
                <ext:DateColumn ColumnID="EndTime" Header="EndTime" DataIndex="EndTime" />
                <ext:DateColumn ColumnID="BuildTime" Header="BuildTime" DataIndex="BuildTime" />
                <ext:Column ColumnID="Status" Header="Status" DataIndex="Status" />
            </Columns>
        </ColumnModel>
        <SelectionModel>
            <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="true" />
        </SelectionModel>
    </ext:GridPanel>