gpt4 book ai didi

c# - 是否可以将条件属性创建为 DisplayIf?

转载 作者:太空狗 更新时间:2023-10-29 23:23:07 25 4
gpt4 key购买 nike

我想创建一个用于我的 View 模型的属性。我想根据第三个值显示不同的文本字符串。

我想做这样的事情......

[DisplayIf("IsPropertyValid", true, Name="value 1")]
[DisplayIf("IsPropertyValid", false, Name="value 2")]
public string MyProperty { get; set; }

public bool IsPropertyValid { get; set; }

根据我的值 IsPropertyValid 是否为真,我想显示一个或另一个。 IE。当属性 IspPropertyValid 等于 true 时,“值 1”将是显示文本,否则它将是“值 2”。

使用 ASPNET.MVC 属性这可能吗?或者甚至更好……像这样的组合……

[DisplayIf("IsPropertyValid", new {"value 1", "value 2"})].
public string MyProperty { get; set; }

public bool IsPropertyValid { get; set; }

然后该属性检查 IsPropertyValid 的值并确保显示的值为“值 1”或“值 2”。

最佳答案

下面是一个如何进行此操作的示例。

我们要做的是创建一个名为Person 的简单类,并显示有关他们的一些基本信息。

一个人有两个属性

  • 姓名
  • 活跃

IsActive 属性是一个 bool 值,将是用于确定用户名显示的属性。

最终我们要做的是将名为 DisplayIf 的新属性应用到 Name 属性。它看起来像这样:

[DisplayIf("IsActive", "This value is true.", "This value is false.")]

首先,让我们创建我们的模型。创建一个名为 Person 的类并将其放入 Models 文件夹中。

模型/Person.cs

public class Person
{
[DisplayIf("IsActive", "This value is true.", "This value is false.")]
public string Name { get; set; }
public bool IsActive { get; set; }
}

创建一个名为Attributes 的文件夹,然后将以下类放入其中:

属性/DisplayIfAttribute.cs

public class DisplayIfAttribute : Attribute
{
private string _propertyName;
private string _trueValue;
private string _falseValue;

public string PropertyName
{
get { return _propertyName; }
}

public string TrueValue
{
get { return _trueValue; }
}

public string FalseValue
{
get { return _falseValue; }
}

public DisplayIfAttribute(string propertyName, string trueValue, string falseValue)
{
_propertyName = propertyName;
_trueValue = trueValue;
_falseValue = falseValue;
}
}

让我们创建一个简单的 Controller 和操作。我们将使用通用的 /Home/Index

Controller /HomeController.cs

public class HomeController : Controller
{
public ActionResult Index()
{
HomeIndexViewModel viewModel = new HomeIndexViewModel();

Person male = new Person() { Name = "Bob Smith", IsActive = true };
Person female = new Person() { Name = "Generic Jane", IsActive = false };

Person[] persons = {male, female};

viewModel.Persons = persons;

return View(viewModel);
}

}

创建一个名为 ViewModels 的新文件夹并创建一个 HomeViewModels.cs 类。

ViewModels/HomeViewModels.cs

public class HomeIndexViewModel
{
public IEnumerable<Person> Persons { get; set; }
}

我们的索引 View 非常简单。

Views/Home/Index.cshtml

@model HomeIndexViewModel

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<div>
@Html.DisplayForModel()
</div>

DisplayForModel 将在您创建此显示模板时起作用:

Views/Home/DisplayTemplates/HomeIndexViewModel.cshtml

@model HomeIndexViewModel

@Html.DisplayFor(m => m.Persons)

DisplayFor -> 当您创建此显示模板时,Persons 将起作用:

Views/Shared/DisplayTemplates/Person.cshtml

@model Person

@foreach (var prop in ViewData.ModelMetadata.Properties)
{
if (prop.HasDisplayIfAttribute())
{
<p>@Html.DisplayIfFor(x => prop)</p>
}
else
{
<p>@Html.DisplayFor(x => prop.Model)</p>
}
}

但是这个展示模板中的这些方法是什么?创建一个名为 Extensions 的新文件夹并添加以下类:

扩展/ModelMetaDataExtensions.cs

public static class ModelMetaDataExtensions
{
public static bool HasDisplayIfAttribute(this ModelMetadata data)
{
var containerType = data.ContainerType;
var containerProperties = containerType.GetProperties();
var thisProperty = containerProperties.SingleOrDefault(x => x.Name == data.PropertyName);
var propertyAttributes = thisProperty.GetCustomAttributes(false);
var displayIfAttribute = propertyAttributes.FirstOrDefault(x => x is DisplayIfAttribute);

return displayIfAttribute != null;
}
}

扩展/HtmlHelperExtensions.cs

public static class HtmlHelperExtensions
{
public static IHtmlString DisplayIfFor<TModel, TProperty>
(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
where TProperty : ModelMetadata
{
string returnValue = string.Empty;

var modelMetaData = expression.Compile().Invoke(helper.ViewData.Model);

var containerType = typeof(TModel);
var containerProperties = containerType.GetProperties();
var propertyInfo = containerProperties
.SingleOrDefault(x => x.Name == modelMetaData.PropertyName);
var attribute = propertyInfo.GetCustomAttributes(false)
.SingleOrDefault(x => x is DisplayIfAttribute) as DisplayIfAttribute;
var conditionalTarget = attribute.PropertyName;

var conditionalTargetValue = (bool)containerType
.GetProperty(conditionalTarget).GetValue(helper.ViewData.Model);

if (conditionalTargetValue)
{
returnValue = attribute.TrueValue;
}
else
{
returnValue = attribute.FalseValue;
}

return MvcHtmlString.Create(returnValue);
}
}

最终输出:

Output

关于c# - 是否可以将条件属性创建为 DisplayIf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20255093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com