Ext.NET: 访问 ColumnModel 中的复杂模型字段

Ext.NET: accessing complex model field in ColumnModel

我在 Ext.NET 中显示专栏时遇到问题。我有以下查看代码:

@(x.Store(
    x.Store()
        .ID("MyStore")
        .AutoLoad(true)
        .Proxy(
            x.AjaxProxy()
                .Url(Url.Action("GetData", "MyController", new { id = Model.Id }))
                .Reader(x.JsonReader().RootProperty("data")))
        .Model(
                x.Model()
                    .Fields(
                        x.ModelField()
                            .Fields(f =>
                            {
                                f.Add("Plan.Value", ModelFieldType.String);
                                f.Add("Plan.IsUrl", ModelFieldType.Boolean);
                            })
                            .IsComplex(true)
                            .Persist(false),
                        x.ModelField()
                            .Name("Id")
                            .Type(ModelFieldType.Int)
                            .Persist(false)))))

@(x.GridPanel()
    .ID("MyPanel")
    .StoreID("MyStore")
    .Selectable(true)
    .HeaderBorders(false)
    .ColumnModel(
        x.Column()
            .DataIndex("Plan.Value")
            .Text("Plan")
            .Width(500)))

我的控制器看起来像:

public class MyController : Controller
{
    private readonly IDataViewModelFactory _storeModelFactory;

    public MyController(IDataViewModelFactory factory)
    {
        if (factory == null)
        {
            throw new ArgumentNullException("factory");
        }

        _storeModelFactory = factory;
    }

    public ActionResult Index(DataListStoreModel model)
    {
        var viewModel = new DataViewModel
        {
            Name = model.Name,
            Id = model.Id
        };

        return View(viewModel);
    }

    public StoreResult GetData(int id)
    {
        var models = _storeModelFactory.GetStoreResults(id);
        return new StoreResult(models);
    }
}

我的模型是:

public class Data
{
    public UrlTextUnion Plan { get; set; }

    public int Id { get; set; }
}

public class UrlTextUnion
{
    public string Value { get; set; }

    public bool IsUrl { get; set; }

    public UrlTextUnion(string value)
    {
        Value = value;
        IsUrl = Uri.IsWellFormedUriString(value, UriKind.Absolute);
    }
}

现在,我知道我正在获取数据,如果我添加一个 JavaScript 侦听器,我可以看到 record.Data.Valuerecord.Data.IsUrl 中有数据,但是列在我的模型中仍然是空的。有人能告诉我这是怎么回事吗?

这里有两个选项。

1.将渲染器添加到 GridPanel 中的列:

保持示例中的 Store,并将 GridPanel 中的 ColumnModel 更改为如下所示:

.ColumnModel(
    Html.X().Column()
        .Renderer(new Renderer("return record.data.Plan.Value"))
        .Text("Plan")
        .Width(500)))

2。修改 Store Model 和 GridPanel 中的字段:

将映射和名称添加到您的商店字段:

Html.X().Model()
    .Fields(
        Html.X().ModelField()
            .Name("PlanValue")
            .Mapping("Plan.Value")
            .Type(ModelFieldType.String)
            .Persist(false),
        Html.X().ModelField()
            .Name("PlanId")
            .Mapping("Plan.Id")
            .Type(ModelFieldType.Int)
            .Persist(false),
        Html.X().ModelField()
            .Name("Id")
            .Type(ModelFieldType.Int)
            .Persist(false)))))

并更改您的 Grid ColumnModel:

.ColumnModel(
    Html.X().Column()
        .DataIndex("PlanValue")
        .Text("Plan")
        .Width(500)))

P.S.

您很可能应该更改:

.Url(Url.Action("GetData", "MyController", new { id = Model.Id }))

.Url(Url.Action("GetData", "My", new { id = Model.Id }))