- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 View ,其中列出了添加到平面图中的表格。表格派生自 TableInputModel
,以允许 RectangleTableInputModel
、CircleTableInputModel
等
ViewModel 有一个 TableInputModel
列表,它们都是派生类型之一。
我有每个派生类型的部分 View ,并且给定混合派生类型的List
,框架知道如何呈现它们。
但是,提交表单后,类型信息会丢失。我尝试过使用自定义模型绑定(bind)器,但由于类型信息在提交时丢失,因此无法工作...
以前有人尝试过吗?
最佳答案
假设您有以下型号:
public abstract class TableInputModel
{
}
public class RectangleTableInputModel : TableInputModel
{
public string Foo { get; set; }
}
public class CircleTableInputModel : TableInputModel
{
public string Bar { get; set; }
}
以及以下 Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new TableInputModel[]
{
new RectangleTableInputModel(),
new CircleTableInputModel()
};
return View(model);
}
[HttpPost]
public ActionResult Index(TableInputModel[] model)
{
return View(model);
}
}
现在您可以编写 View 了。
主视图Index.cshtml
:
@model TableInputModel[]
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
以及相应的编辑器模板。
~/Views/Home/EditorTemplates/RectangleTableInputModel.cshtml
:
@model RectangleTableInputModel
<h3>Rectangle</h3>
@Html.Hidden("ModelType", Model.GetType())
@Html.EditorFor(x => x.Foo)
~/Views/Home/EditorTemplates/CircleTableInputModel.cshtml
:
@model CircleTableInputModel
<h3>Circle</h3>
@Html.Hidden("ModelType", Model.GetType())
@Html.EditorFor(x => x.Bar)
最后缺少的和平是 TableInputModel
类型的自定义模型绑定(bind)器,它将使用发布的隐藏字段值来获取类型并实例化正确的实现:
public class TableInputModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType");
var type = Type.GetType(
(string)typeValue.ConvertTo(typeof(string)),
true
);
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
将在Application_Start
中注册:
ModelBinders.Binders.Add(typeof(TableInputModel), new TableInputModelBinder());
这就是全部了。现在,在 Index Post 操作中,模型数组将使用正确的类型正确初始化。
关于asp.net-mvc - 带有 List<BaseClass> 和编辑器模板的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6484972/
在我的游戏中,我有一个 Loot 基类,它具有对任何可以被玩家捡起并存储在他的元素栏中的东西通用的方法。这将包括药水、设备、弹药等。我可以装备箭,但不能装备药水。所以 Arrow 将是 Ammo 的一
有 class BaseClass implements IData (); class ChildClassA() extends BaseClass; class ChildClassB() ex
例如,在 vanilla JS 中我可以轻松地执行以下操作: class BaseClass extends React.Component { ... } class Foo extends Bas
我正在自动化一个由多个独立应用程序组成的环境。在某些时候,我决定将每个应用程序定义为一个类并将其保存为单独的文件是最有意义的。 我现在拥有的: 包含 *py 文件的目录,其中每个文件为单个应用程序定义
我有一个基类和许多子类。在每个子类中,我都使用对象 A - 因此我将它放在基类中。在一半的子类中,我使用对象 B,因此我没有将 B 放在基类中。我希望我的基类包含尽可能多的功能,因此如果定义了对象 B
我有: class BASE{ public: virtual void func1() = 0; }; 然后我有一些派生类,比如: class Derived1 : public BASE{
(抱歉标题模糊) 我有一个带有各种派生类 D1、D2 等的基类 B。 另外还有一个模板 template Storage; 现在每个派生类都有不同版本的存储类 class D1 : public c
请考虑以下类别: public interface BaseInterface { void method1(); void method2(); void method3()
我无法在具有 2 个输入引脚的 BaseClasses 中找到合适的渲染器类 - 一个用于视频,一个用于音频。有人可以推荐/提供一些代码吗? 问候多米尼克 最佳答案 CBaseRenderer cla
我正在学习一门新的 C++ 语言,其中使用了 Java 中使用的知识 not JavaScript .我正在尝试创建一个名为 Command 的父类派生类 CmdHelp .在 Java 中我会制作一
基本上我有一个名为 Asset 的类,它包含我系统中某个 Asset 的所有信息。这可能会变得相当大( Assets 有缩略图、文件名、元数据、评级、评论等)。 在我的结果页面上,我列出了所有符合特定
我正在尝试创建一个简单的元素“ list ”,就像在任何角色扮演游戏中一样。我制作了非常基本的类,它们具有属性。 无论如何,我有一个基类 item从那里继承的是weapon . item具有也在 we
我即将为我的 Razor 组件创建一个基类。 这个 Base-Class 看起来像: public abstract class ExampleBase : ComponentBase { p
我正在开发一款可以追溯到很久以前的软件产品。我们源代码的一部分是构建基类的项目。据推测,这是一个随 Windows SDK 一起提供的项目,用于开发引用 DirectShow 的应用程序。 不幸的是,
Internet 上有很多关于在覆盖 Equals 时如何覆盖 GetHashCode() 的信息。但是,所有这些示例都是关于包含一些可以生成哈希的字段的类。我试图找到的是一个很好的 GetHashC
我有很多类都继承自一个基类——实际上它只是一个允许序列化的接口(interface)包装器: public abstract class VoucherRuleBase : IRule{ pu
我的 java 程序中有一系列类,如下所示: src | |- BaseCost |- com.jdoe.nationalcosts |- EntryTicketCost |- Fli
编辑:这是我的错,因为还有另一个 Foo潜伏的定义让我对Foo<>感到困惑定义。 在下面的代码中,Foo是 Bar 的基类对吧? 那为什么编译器会在 .BaseType 下划线呢?部分并说“给定的表达
我有一个名为 GLObject 的基类,具有以下 header : class GLObject{ public: GLObject(float width = 0.0, fl
命名基类的推荐方法是什么?是在类型名称前加上“Base”或“Abstract”还是我们只在其后缀“Base”? 考虑以下几点: 类型:ViewModel 例如MainViewModel、ReportV
我是一名优秀的程序员,十分优秀!