使用Kentico 12 MVC,我创建了一个自定义表,用于存储用于填充视图中下拉列表的信息。
它名为MyCustom.Tables,并添加了带有相关信息的“ ProgramName”和“ ProgramID”字段
如何将它们传递给我?我在控制器中尝试过:
IEnumerable<ProgramList> allPrograms = CustomTableItemProvider.GetItems<ProgramList>("MyCustom.Tables");
,然后在我看来:
@Html.DropDownListFor(m => m.Data.ProgramSelected, new SelectList(Model.Data.AllPrograms, "ProgramID", "ProgramName"), "- Please Select -", new { @class = "browser-default" })
具有以下型号
public class ProgramList
{
public string ProgramID { get; set; }
public string ProgramName { get; set; }
}
但是我在控制器中出现错误(方法GetItems的重载没有1个参数)...
我还尝试将控制器更改为
IEnumerable allPrograms = CustomTableItemProvider.GetItems("MyCustom.Tables");
但是在这种情况下,我的自定义表中的自定义字段不可用,只有默认的ItemID Kentico字段可用。
有什么想法吗?
S。
########################我添加了Kentico生成的以下代码:
using System;
using System.Collections.Generic;
using CMS;
using CMS.Base;
using CMS.Helpers;
using CMS.DataEngine;
using CMS.CustomTables.Types.MySite;
using CMS.CustomTables;
[assembly: RegisterCustomTable(ProgramsItem.CLASS_NAME, typeof(ProgramsItem))]
namespace CMS.CustomTables.Types.MySite
{
/// <summary>
/// Represents a content item of type ProgramsItem.
/// </summary>
public partial class ProgramsItem : CustomTableItem
{
#region "Constants and variables"
/// <summary>
/// The name of the data class.
/// </summary>
public const string CLASS_NAME = "MySite.Programs";
/// <summary>
/// The instance of the class that provides extended API for working with ProgramsItem fields.
/// </summary>
private readonly ProgramsItemFields mFields;
#endregion
#region "Properties"
/// <summary>
/// Name.
/// </summary>
[DatabaseField]
public string ProgramName
{
get
{
return ValidationHelper.GetString(GetValue("ProgramName"), @"");
}
set
{
SetValue("ProgramName", value);
}
}
/// <summary>
/// Gets an object that provides extended API for working with ProgramsItem fields.
/// </summary>
[RegisterProperty]
public ProgramsItemFields Fields
{
get
{
return mFields;
}
}
/// <summary>
/// Provides extended API for working with ProgramsItem fields.
/// </summary>
[RegisterAllProperties]
public partial class ProgramsItemFields : AbstractHierarchicalObject<ProgramsItemFields>
{
/// <summary>
/// The content item of type ProgramsItem that is a target of the extended API.
/// </summary>
private readonly ProgramsItem mInstance;
/// <summary>
/// Initializes a new instance of the <see cref="ProgramsItemFields" /> class with the specified content item of type ProgramsItem.
/// </summary>
/// <param name="instance">The content item of type ProgramsItem that is a target of the extended API.</param>
public ProgramsItemFields(ProgramsItem instance)
{
mInstance = instance;
}
/// <summary>
/// Name.
/// </summary>
public string ProgramName
{
get
{
return mInstance.ProgramName;
}
set
{
mInstance.ProgramName = value;
}
}
}
#endregion
#region "Constructors"
/// <summary>
/// Initializes a new instance of the <see cref="ProgramsItem" /> class.
/// </summary>
public ProgramsItem() : base(CLASS_NAME)
{
mFields = new ProgramsItemFields(this);
}
#endregion
}
}
并按照此处https://docs.kentico.com/k12/developing-websites/retrieving-content-in-mvc-applications的建议尝试了愚蠢的代码,但暂时无法正常工作:
IEnumerable<ProgramsItem> allPrograms = CustomTableItemProvider.GetItems<ProgramsItem>();
ProgramsItem item = CustomTableItemProvider.GetItem<ProgramsItem>(1);
例如上面的项目在我的自定义表中有记录时返回null。
基于https://www.bizstream.com/blog/may-2019/powerful-kentico-custom-table-visualization,我也尝试过
IEnumerable<ProgramList> GetAllPrograms = CustomTableItemProvider.GetItems<ProgramsItem>()
.Select(program => new ProgramList
{
ProgramName = program.ProgramName
});
但再次获得:
无法将类型为“ CMS.CustomTables.CustomTableItem”的对象转换为 键入“ CMS.CustomTables.Types.MySite.ProgramsItem”。
谢谢
答案 0 :(得分:1)
如果ProgramList是从自定义代码表(自定义表->编辑->代码->保存代码并将其包含在解决方案中)生成的代码,则应在不提供className的情况下调用它:
CustomTableItemProvider.GetItems<ProgramList>();
这将使您可以直接访问自定义表的属性(强键入结果)。
如果ProgramList是您的自定义类(如所述),则应仅使用className进行调用(不指定对象类型)。要访问自定义项目,请使用GetValue(GetStringValue,GetBooleanValue等),例如:
allPrograms[0].GetValue("CustomFieldname")
答案 1 :(得分:0)
以下讨论:https://devnet.kentico.com/questions/unable-to-cast-object-of-type-cms-customtables-customtableitem-to-type和https://docs.kentico.com/k12/developing-websites/generating-classes-for-kentico-objects
我必须添加
using CMS;
[assembly: AssemblyDiscoverable]
在我生成的类中,以使其可访问。
IEnumerable<ProgramList> GetAllPrograms = CustomTableItemProvider.GetItems<ProgramsItem>()
.Select(program => new ProgramList
{
ProgramName = program.ProgramName
});
现在工作正常。
S。