MVC Ext.NET, cómo crear un enlace de datos a partir de un modelo

I am using MVC3 + Ext.NET controllers

this is my Model:

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

...

This is my Controller (nothing Special)

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

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

Esta es mi vista:

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

The problem that i have is that i cannot bind the Subsystem Class into the controller, it is working only when i create it as an Object. This is not working for me:

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

It is working as expected only when i create it like this:

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

I do not want to create all of my classes as Objects, i want to create various classes such as Subsystem, Employee, Person etc. and than to be able to bind them into the controller Can this be done?

preguntado el 01 de febrero de 12 a las 14:02

How about changing your ArrayReader to a JsonReader? -

Have you tried solution below? -

1 Respuestas

You have to use JsonReader and write names of fields in correct case.

Captura de pantalla

Try this code for your 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>

Respondido 02 Feb 12, 18:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.