【问题标题】:Have Required Attribute apply to property in one view and not another? [closed]必需属性是否适用于一个视图而不是另一个视图中的属性? [关闭]
【发布时间】:2017-04-07 14:14:35
【问题描述】:

假设我有一个名为 Test 的类,并且我有一个 Create and Edit 视图。说这个类非常简单

public class Test
{
    [Required]
    public string str { get; set; }
}

是否可以在用户编辑此对象时删除所需的属性?

【问题讨论】:

  • asp.net mvc,其他?

标签: c# asp.net-mvc


【解决方案1】:

ViewModels 就是为了这个。一个用于创建,一个用于编辑

您应该为此使用 ViewModel,因为您需要在此处查看特定模型:

public class CreateTestViewModel
{
    [Required]
    public string str { get; set; }
}

和:

public class EditTestViewModel
{
    public string str { get; set; }
}

您可能想了解What is ViewModel in MVCHow to use ViewModel in MVC

【讨论】:

  • 如果 EditTestViewModel 继承自 CreateTestViewModel,是否可以在不需要它的情况下覆盖 str,或者这里的正确解决方案是完全分离类?
  • 它们应该是分开的,不能继承:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2013-08-14
  • 2014-02-21
  • 2012-12-30
  • 1970-01-01
相关资源
最近更新 更多