我如何从 dnn 中的数据库填充下拉列表

How can i populate dropdown fom database in dnn

我想在 DNN 站点中添加新页面,该页面从数据库中填充国家和州的两个下拉列表。

请建议我们如何做到这一点?

在这里,我将向您展示如何使用 DNN 模块创建动态页面。有关详细信息,请参阅 this link

  1. 首先在Visual Studio打开你的DNN网站。您可以在您的根目录中看到 DesktopModules 文件夹,右键单击该目录并添加新目录并将其命名为您的模块名称,例如 "CountryState"。现在右键单击 CountryState 文件夹并添加->添加新项->Select Web 用户控件 并将其命名为你的模块名称如 "CountryState.ascx",它将显示 CountryState.ascx 和 CountryState.ascx.cs 文件在 DesktopModules/CountryState 文件夹中。
  2. 下一步将继承 class "System.Web.UI.UserControl" 更新为 "PortalModuleBase"。这是因为 PortalModuleBase class 定义了一个由门户中的所有桌面门户模块继承的自定义基础 class。 PortalModuleBase class 定义了门户框架用来正确显示门户模块的门户特定属性。
  3. 现在使用您的主机凭据登录您的网站。
  4. 在 DNN 面板中,转到主机 -> 扩展。
  5. 单击扩展页面右上角的创建新模块按钮。
  6. 创建新模块按钮打开一个弹出窗口 window 和 创建模块表单 DropDownList。 Select 从下拉菜单中控制
  7. 它将显示您的表单,select 您的 .ascx 网络用户控件,从相应的文件夹路径并单击 Create Module 按钮。
  8. 终于在您的页面上添加了拖放模块。
  9. 尽情享受吧! ;)

我有一篇博客文章,其中包含有关如何在 DNN 中完成此操作的代码示例。

CASCADING COUNTRY / STATE DROPDOWNS: A STUDY IN DOTNETNUKE 7 WEBAPI SERVICES AND AJAX

Keval 的解决方案对于快速而肮脏的解决方案来说还可以,但对于更具可持续性的解决方案,除了 DNN 之外,我更愿意将我的模块保留在它们自己的解决方案中。然后我使用 project post build events 将所需代码复制到 bin 和桌面模块文件夹。这使得源代码控制更容易,并更快地打开解决方案。

不同文件扩展名的构建脚本

xcopy "$(ProjectDir)*.ascx" C:\Webs\Properteez\DesktopModules$(ProjectName)\*.ascx   /S /C /Y /D
xcopy "$(ProjectDir)*.css" C:\Webs\Properteez\DesktopModules$(ProjectName)\*.css   /S /C /Y /D
xcopy "$(ProjectDir)*.txt" C:\Webs\Properteez\DesktopModules$(ProjectName)\*.txt   /S /C /Y /D
xcopy "$(ProjectDir)App_LocalResources\*.resx" C:\Webs\Properteez\DesktopModules$(ProjectName)\App_LocalResources\*.resx   /S /C /Y /D
xcopy "$(ProjectDir)Styles\*.css" C:\Webs\Properteez\DesktopModules$(ProjectName)\Styles\*.css   /S /C /Y /D
xcopy "$(ProjectDir)Scripts\*.js" C:\Webs\Properteez\DesktopModules$(ProjectName)\Scripts\*.js   /S /C /Y /D
xcopy "$(ProjectDir)Images\*.*" C:\Webs\Properteez\DesktopModules$(ProjectName)\Images\*.*   /S /C /Y /D
xcopy "$(TargetDir)$(TargetFileName)" C:\Webs\Properteez\bin   /C /Y /Q /D

此外,

populates two dropdown for Country and State from the database

可以使用 PetaPoco "DNN way" 与数据库通信。

模型示例(这需要直接映射您的数据库 table):

#region Usings

using System;
using DotNetNuke.ComponentModel.DataAnnotations;
using DotNetNuke.Common.Utilities;

#endregion

namespace Properteez.Data.Entities
{
    [TableName("Sync_Property")]
    [PrimaryKey("PropertyId", "PropertyId")]
    public class Property
    {
        #region Properties

        public int PropertyId { get; set; }
        public string Name { get; set; }
        public string ShortDesc { get; set; }
        public int UserId { get; set; }
        public bool IsDeleted { get; set; }
// You can use the [IgnoreColumn] attribute if a property does not map to a DB field
    }
}

存储库方法示例:

    public static Property Get(int id)
    {
        Property item;
        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            item = rep.GetById(id);
        }
        return item;
    }

    public static List<Property> GetAllSinceLastSync(DateTime lastSyncDate)
    {
        List<Property> items;
        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            items = rep.Get()
                // Filter list to valid properteez that have been added or changed since last sync
                .Where(p => (!p.IsDeleted && !p.IsSold) && p.LastModifiedOnDate >= lastSyncDate)
                .ToList();
        }
        return items;
    }

    public static List<Property> GetAll()
    {
        List<Property> items;
        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            items = rep.Get().ToList();
        }
        return items;
    }

    public static int Create(Property item)
    {
        item.CreatedOnDate = DateTime.Now;
        item.LastModifiedOnDate = DateTime.Now;

        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            rep.Insert(item);
        }
        return item.PropertyId;
    }

    public static void Update(Property item)
    {
        item.LastModifiedOnDate = DateTime.Now;

        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            rep.Update(item);
        }
    }

    public static void Delete(Property item)
    {
        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<Property>();
            rep.Delete(item);
        }
    }

调试

您可以通过构建(将 .dll 复制到 DNN 实例的 bin 文件夹)调试应用程序,转到调试>附加到进程并选择 "W3wp" 进程

请注意,您需要 运行 Visual Studio 作为管理员。