gpt4 book ai didi

asp.net-mvc - 对相似模型的 Controller 操作使用相同的 View

转载 作者:行者123 更新时间:2023-12-03 04:40:38 25 4
gpt4 key购买 nike

我有很多具有相同格式的模型(例如以下模型,只有 Id 和 Name 属性),它们全部继承自实体模型

  public abstract class Entity 
{
public int Id { get; set; }
}
public class Exapmle1 : Entity
{
public string Name { get; set; }
}
public class Exapmle2 : Entity
{
public string Name { get; set; }
}
public class Exapmle3 : Entity
{
public string Name { get; set; }
}

我不喜欢在 CRUD 操作中为每个模型实现多个 Controller 和对应 View 。有没有办法获得最低限度的实现?
例如,对于使用相同格式模型的所有已实现的 Controller ,只有一个索引 View (列表)。

最佳答案

最后我发现通用 Controller 和共享 View 的结合是最好的方法。在这种情况下,还可以对每个 Controller 单独进行身份验证。

通用 Controller :

 public abstract class GenericController<T> : Controller
where T : BaseInformation
{ //Controller Implementation for Index, Create, Edit, Delete }

在共享文件夹中查看

@model BaseInformation
//Model Implementation

最终控制者

[Authorize(Roles = "Admin")]
public class Base1Controller : GenericController<Base1>
{
}
[Authorize(Roles = "Helpdesk")]
public class Base2Controller : GenericController<Base2>
{
}

关于asp.net-mvc - 对相似模型的 Controller 操作使用相同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987743/

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