对类似模型的控制器操作使用相同的视图
Using same view for controller actions with similar models
我有很多相同格式的模型(像下面的模型,只是有 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 操作中为每个模型实现多个控制器和对应视图。
有什么方法可以实现最低限度的实现吗??
例如,对于所有使用相同格式模型的已实现控制器,只有 一个 索引视图(列表)。
您可以使用具有动态视图模型的视图和接受实体的控制器操作(您的基础 class)。它可能看起来像这样
控制器:
public ActionResult Index(Entity foo)
{
if(foo is Example1)
{
var e1 = foo as Example1;
//do your stuff
}
return View();
}
查看:
@model dynamic
@{
ViewBag.Title = "IndexNotStonglyTyped";
}
<h2>Index Not Stongly Typed</h2>
<p>
<label>@Model.Name</label>
<label>@Model.Id</label>
</p>
当您拥有密切相关的实体组(或与此相关的任何其他对象)时,一种有效的方法是将它们拆分为可组合的接口,例如:
public interface IIdIdentity
{
int Id { get; set; }
}
public interface INameIdentity
{
int Name { get; set; }
}
public interface IYourGroup : IIdIdentity, INameIdentity
{
}
public class Exapmle1 : IYourGroup
{
public int Id { get; set; }
public int Name { get; set; }
}
然后您的视图可以接受任何类型的实体 IYourGroup
,前提是您的域实体满足接口。
最后我发现结合使用通用控制器和 共享 视图是最好的方法。在这种情况下,还可以分别对每个控制器进行身份验证。
通用控制器:
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>
{
}
我有很多相同格式的模型(像下面的模型,只是有 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 操作中为每个模型实现多个控制器和对应视图。
有什么方法可以实现最低限度的实现吗??
例如,对于所有使用相同格式模型的已实现控制器,只有 一个 索引视图(列表)。
您可以使用具有动态视图模型的视图和接受实体的控制器操作(您的基础 class)。它可能看起来像这样
控制器:
public ActionResult Index(Entity foo)
{
if(foo is Example1)
{
var e1 = foo as Example1;
//do your stuff
}
return View();
}
查看:
@model dynamic
@{
ViewBag.Title = "IndexNotStonglyTyped";
}
<h2>Index Not Stongly Typed</h2>
<p>
<label>@Model.Name</label>
<label>@Model.Id</label>
</p>
当您拥有密切相关的实体组(或与此相关的任何其他对象)时,一种有效的方法是将它们拆分为可组合的接口,例如:
public interface IIdIdentity
{
int Id { get; set; }
}
public interface INameIdentity
{
int Name { get; set; }
}
public interface IYourGroup : IIdIdentity, INameIdentity
{
}
public class Exapmle1 : IYourGroup
{
public int Id { get; set; }
public int Name { get; set; }
}
然后您的视图可以接受任何类型的实体 IYourGroup
,前提是您的域实体满足接口。
最后我发现结合使用通用控制器和 共享 视图是最好的方法。在这种情况下,还可以分别对每个控制器进行身份验证。
通用控制器:
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>
{
}