【问题标题】:How to set controls visibility dynamically in MVC depending on database records如何根据数据库记录在 MVC 中动态设置控件可见性
【发布时间】:2016-11-24 09:06:06
【问题描述】:

我正在寻找根据数据库列可见性选项设置为真或假来设置我的控件可见性。我想动态设置控件的可见性。我正在考虑使用 CustomAttributes 并使用它设置 ViewModel。但我不知道怎么做。一个人的起点,帮我开始。

[Visible]
public string FullName { get; set; }

【问题讨论】:

  • 我想使用CustomAttribute,这个解决方案与UI几乎没有耦合。
  • 你说的CustomAttribute是什么意思?
  • 我已经编辑了我的问题
  • 好吧,如果您使用数据注释属性,您还必须编写客户 Html Helpers。一个简单的任务可能需要做很多工作;它是由你决定。 How do Data Annotations work?

标签: c# asp.net asp.net-mvc viewmodel custom-attributes


【解决方案1】:

你可以像这样用 Razor 写一个简单的@helper

App_Code\MyHelpers.cshtml

@helper DisplayIt(object value, bool visibility)
{
    if (!visibility){return;}

    <span>@value</span>
}

然后在你的视图中这样使用它:

@MyHelpers.DisplayIt(@Model.FullName, @Model.Visible)
@MyHelpers.DisplayIt(@Model.Email, @Model.Visible)
@MyHelpers.DisplayIt(@Model.Tel, @Model.Visible)

【讨论】:

    【解决方案2】:

    您必须执行以下步骤:

    第 1 步:创建自定义属性

    public class VisibilityAttribute : ValidationAttribute
    {
        private bool _isVisible;
    
        public VisibilityAttribute(bool visible = true)
        {
            _isVisible = visible;
        }
    
        public bool Status
        {
            get
            {
                return _isVisible;
            }
            set
            {
                _isVisible = value;
            }
        }
    }
    

    第 2 步:向模型添加自定义属性

    [Visibility(Status = false)]
    public string FullName { get; set; }
    

    第 3 步:创建自定义 Html Helper

    public static class CustomHtmlExtensions
    {
        public static MvcHtmlString CustomDisplayFor<TModel, TResult>(this HtmlHelper<TModel> html,
            Expression<Func<TModel, TResult>> expression)
        {
            ExpressionType type = expression.Body.NodeType;
            if (type == ExpressionType.MemberAccess)
            {
                MemberExpression memberExpression = (MemberExpression)expression.Body;
                PropertyInfo pi = memberExpression.Member as PropertyInfo;
    
                var attributes = pi.GetCustomAttributes();
    
                foreach (var attribute in attributes)
                {
    
                    if (attribute is VisibilityAttribute)
                    {
                        VisibilityAttribute vi = attribute as VisibilityAttribute;
                        if (vi.Status)
                        {
                            var metadata = ModelMetadata.FromLambdaExpression<TModel, TResult>(expression, html.ViewData);
                            return MvcHtmlString.Create(metadata.SimpleDisplayText);
                        }
                    }
                }
            }
            return MvcHtmlString.Create("");
        }
    }
    

    第 4 步:在视图中使用自定义 Html Helper

     @Html.CustomDisplayFor(model => model.FullName)
    

    【讨论】:

      【解决方案3】:

      我的比阿里的回答稍微简单:

      在您的模型类中:

      public class Client
      {
          [Visible]
          public string FullName { get; set; }
      }
      

      添加扩展方法 VisibleLabelFor

      public static class HtmlExtensions
      {
          public static MvcHtmlString VisibleLabelFor<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression)
          {
              var type = expression.Body.NodeType;
      
              if (type == ExpressionType.MemberAccess)
              {
                  var memberExpression = (MemberExpression) expression.Body;
                  var p = memberExpression.Member as PropertyInfo;
      
                  if (!Attribute.IsDefined(p, typeof (VisibleAttribute)))
                      return new MvcHtmlString(string.Empty);
      
                  return html.LabelFor(expression);
              }
          }
      }
      

      那么在你看来:

      @Html.VisibleLabelFor(m => m.FullName)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 2012-07-25
        • 2011-07-13
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多