gpt4 book ai didi

asp.net-mvc - 带有 List 和编辑器模板的 ViewModel

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

我有一个 View ,其中列出了添加到平面图中的表格。表格派生自 TableInputModel,以允许 RectangleTableInputModelCircleTableInputModel

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/

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