asp.net-mvc - 从 Kentico 自定义表中填充我的 MVC 视图中的下拉列表
问题描述
使用 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
}
}
并尝试这里建议的 foowing 代码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”类型。
谢谢
解决方案
如果 ProgramList 是从自定义代码表生成的代码(自定义表 -> 编辑 -> 代码 -> 保存代码并将其包含在解决方案中),那么您应该在不提供类名的情况下调用它:
CustomTableItemProvider.GetItems<ProgramList>();
这将允许您直接访问自定义表的属性(强类型结果)。
如果 ProgramList 是您的自定义类,如问题中所述,那么您应该只使用 className 调用它(不指定对象类型)。要访问自定义项目,请使用 GetValue(GetStringValue、GetBooleanValue 等),例如:
allPrograms[0].GetValue("CustomFieldname")
推荐阅读
- java - 构建中止:没有这样的包'@antlr4_tool//jar'
- sql - 如何处理在 SQL 中返回多个表(结果集)的存储过程
- python - 使用python计算一个人在香港需要缴纳多少税
- javascript - 如何在 JavaScript 中以循环排列方式打印数组?
- mongodb - MongoDb Aggregate , find duplicate records within 7 days
- office365 - Word 在功能区中添加文本框(xml 文件)在 office 中添加。使用 office.js
- java - Java 运行缓慢
- flutter - 如何在不按任何按钮的情况下自动在 Flutter Android 应用程序的 ListView 内实现 AlertDialog 框?
- ios - 是否应该重写 deinit 以删除 Swift 中的观察者?
- java - Rest API 返回 byte[] i 响应