【问题标题】:Best practice for binding drop down lists in 3-tier architecture在 3 层架构中绑定下拉列表的最佳实践
【发布时间】:2014-11-12 11:49:23
【问题描述】:

我是在开发中使用 3 层方法的初学者,我的应用程序中有许多下拉列表,每个下拉列表包含 4 或 5 个选项。如果我将这些数据存储在我的数据库中,那么根据我对 3 层方法的理解,我需要为每个列表创建一个数据访问类和一个业务类。这意味着我需要为 20 个下拉列表创建近 40 个类,这听起来肯定不切实际。

有没有更好的方法来设计下拉列表或在我的应用程序中存储 DDL 数据?

【问题讨论】:

  • 您不需要为每个下拉菜单创建类。您只需创建一个 DAL 和 BAL 即可。在这些类文件中创建函数并根据您的需要使用!

标签: c# asp.net 3-tier


【解决方案1】:

你可以有一个通用的绑定类如下。

/// <summary>
    /// Common DropDown model
    /// </summary>
    public class SelectListModel
    {
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public int Value { get; set; }

        /// <summary>
        /// Gets or sets the item.
        /// </summary>
        /// <value>
        /// The item.
        /// </value>
        public string Item { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsSelected { get; set; }
    }

您可以将任何下拉数据绑定到此模型中并将其返回给视图。

【讨论】:

    【解决方案2】:
    private void DropwonlistBind(DropDownList DDLName)
    {
    
    DDLName.Datasource="YourSouce";
    DDLName.DataTextField="TextField";
    DDLName.DataValueField="IDField";
    DDLName.Databind();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2011-12-14
      • 2011-11-22
      • 2010-10-23
      • 2017-08-24
      相关资源
      最近更新 更多