如何从 @Html.EditForModel() 中排除字段,但使用 Html.DisplayForModel() 显示该字段

发布于 2024-10-31 04:21:47 字数 327 浏览 1 评论 0原文

我正在阅读 ASP.NET MVC 及其所有有趣的用途,我刚刚发现了 数据模板

为了急于测试这个东西,我将一个更简单的模型转换为使用 @Html.DisplayForModel() 和 @Html.EditForModel() ,它的工作原理如下这是一个幸运的魅力:)

我立即发现的一件事是,我无法轻松定义一个字段以显示在显示视图上,但根本无法进行编辑......

I am reading up on ASP.NET MVC and all of it's fun uses and I just found out about DataTemplates.

In my hurry to test this thing out, I converted one of my simpler models over to using @Html.DisplayForModel() and @Html.EditForModel() and it worked like a lucky charm that it is :)

One thing that I immediately found out though was that I could not easily define a field to show up on display views but not be present at all for editing...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜深人未静 2024-11-07 04:21:47

您可以使用 IMetadataAware 接口的创建属性,该属性将在元数据中设置 ShowForEdit 和 ShowForDislay:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class TemplatesVisibilityAttribute : Attribute, IMetadataAware
{
    public bool ShowForDisplay { get; set; }

    public bool ShowForEdit { get; set; }

    public TemplatesVisibilityAttribut()
    {
        this.ShowForDisplay = true;
        this.ShowForEdit = true;
    }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        if (metadata == null)
        {
            throw new ArgumentNullException("metadata");
        }

        metadata.ShowForDisplay = this.ShowForDisplay;
        metadata.ShowForEdit = this.ShowForEdit;
    }

}

然后您可以将其附加到您的属性,如下所示:

public class TemplateViewModel
{
  [TemplatesVisibility(ShowForEdit = false)]
  public string ShowForDisplayProperty { get; set; }

  public string ShowAlwaysProperty { get; set; }
}

这就是您所需要的。

You can make use of IMetadataAware interface an create attribute which will set ShowForEdit and ShowForDislay in Metadata:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class TemplatesVisibilityAttribute : Attribute, IMetadataAware
{
    public bool ShowForDisplay { get; set; }

    public bool ShowForEdit { get; set; }

    public TemplatesVisibilityAttribut()
    {
        this.ShowForDisplay = true;
        this.ShowForEdit = true;
    }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        if (metadata == null)
        {
            throw new ArgumentNullException("metadata");
        }

        metadata.ShowForDisplay = this.ShowForDisplay;
        metadata.ShowForEdit = this.ShowForEdit;
    }

}

Then you can attach it to your property like this:

public class TemplateViewModel
{
  [TemplatesVisibility(ShowForEdit = false)]
  public string ShowForDisplayProperty { get; set; }

  public string ShowAlwaysProperty { get; set; }
}

And this is all you need.

梦忆晨望 2024-11-07 04:21:47

您可以编写自定义元数据提供程序并设置 ShowForEdit 元数据属性。因此,从自定义属性开始:

public class ShowForEditAttribute : Attribute
{
    public ShowForEditAttribute(bool show)
    {
        Show = show;
    }

    public bool Show { get; private set; }
}

然后是自定义模型元数据提供程序:

public class MyModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(
        IEnumerable<Attribute> attributes,
        Type containerType, 
        Func<object> modelAccessor, 
        Type modelType, 
        string propertyName
    )
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        var sfea = attributes.OfType<ShowForEditAttribute>().FirstOrDefault();
        if (sfea != null)
        {
            metadata.ShowForEdit = sfea.Show;
        }
        return metadata;
    }
}

然后在 Application_Start 中注册此提供程序:

ModelMetadataProviders.Current = new MyModelMetadataProvider();

最后装饰:

public class MyViewModel
{
    [ShowForEdit(false)]
    public string Prop1 { get; set; }

    public string Prop2 { get; set; }
}

现在,如果在您的视图中您有:

@model MyViewModel

<h2>Editor</h2>
@Html.EditorForModel()

<h2>Display</h2>
@Html.DisplayForModel()

Prop1 属性不会包含在编辑器模板中。

备注:您可以对 ShowForDisplay 元数据属性执行相同的操作。

You could write a custom metadata provider and set the ShowForEdit metadata property. So start with a custom attribute:

public class ShowForEditAttribute : Attribute
{
    public ShowForEditAttribute(bool show)
    {
        Show = show;
    }

    public bool Show { get; private set; }
}

then a custom model metadata provider:

public class MyModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(
        IEnumerable<Attribute> attributes,
        Type containerType, 
        Func<object> modelAccessor, 
        Type modelType, 
        string propertyName
    )
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        var sfea = attributes.OfType<ShowForEditAttribute>().FirstOrDefault();
        if (sfea != null)
        {
            metadata.ShowForEdit = sfea.Show;
        }
        return metadata;
    }
}

then register this provider in Application_Start:

ModelMetadataProviders.Current = new MyModelMetadataProvider();

and finally decorate:

public class MyViewModel
{
    [ShowForEdit(false)]
    public string Prop1 { get; set; }

    public string Prop2 { get; set; }
}

Now if in your view you have:

@model MyViewModel

<h2>Editor</h2>
@Html.EditorForModel()

<h2>Display</h2>
@Html.DisplayForModel()

the Prop1 property won't be included in the editor template.

Remark: you could do the same with the ShowForDisplay metadata property.

别挽留 2024-11-07 04:21:47

您可以使用 Html.DisplayTextbox 或其他选项之一显示您想要的每个字段吗?这样您还可以自定义引用该字段的外观和标签。

Can you display each of the fields you want using Html.DisplayTextbox or one of the other options? That way you can also customize the appearance and labels referring to the field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文