MVC 4重用视图和视图模型的最佳实践

时间:2021-04-15 23:08:44

I have 3 screens that share a section(with model data in it(@Html.TextBoxFor)). What is the best way to implement this screens?

我有3个屏幕共享一个部分(其中包含模型数据(@Html.TextBoxFor)。实现这个屏幕的最佳方式是什么?

What I tried:

我试着什么:

1) Partial view for the common section(_ClientData). 3 views for the different screens. 3 view models that have common property(ClientData), that is the view model of the partial. Problem: If I pass the model to the partial as @{Html.RenderPartial("_ClientData", Model.ClientData);} the data from the partial is not submited to the model. If I pass the model to the partial as @{Html.RenderPartial("_ClientData", Model);} and reference the properties with a fill name the data is submited, but I can't pass models with different types to the partial view.

1)公共部分(_ClientData)的部分视图。不同屏幕的3个视图。有公共属性的视图模型(ClientData),这是部分的视图模型。问题:如果我将模型传递给部分为@{Html。RenderPartial(“_ClientData”,模型. clientdata);从局部数据中获取的数据并没有被提交到模型中。如果我将模型传递给部分为@{Html。RenderPartial(“_ClientData”,Model);}并使用填充名称引用属性,数据被submited,但是我不能将具有不同类型的模型传递给分部视图。

2) Use one big View model with all the data required by the 3 screens, one view and show/hide some elements depending on some flag. Problem: I can't use ValidationAttributes(for example if one field is required in screen 1, but it's not shown in screen 2 and its value is null, the validation will fire). I can use some manual validation in the controller but the whole thing with the all in one view and viewmodel sounds very bad.

2)使用一个大视图模型,包含3个屏幕所需的所有数据,一个视图,根据某个标志显示/隐藏一些元素。问题:我不能使用ValidationAttributes(例如,如果屏幕1中需要一个字段,但它在屏幕2中没有显示,并且它的值为null,验证就会启动)。我可以在控制器中使用一些手动验证,但是在一个视图和viewmodel中使用all的整个操作听起来很糟糕。

1 个解决方案

#1


1  

Partials are usually not the best choice in case you want to place them inside one form and submit together. In such scenario it is better to take advantage of EditorTemplates which will solve your problem.

如果您想把它们放在一个表单中并一起提交,那么Partials通常不是最好的选择。在这种情况下,最好利用编辑器模板来解决您的问题。

Firstly you would have to drag your partials to the folder ~/Shared/EditorTemplates/ and rename them to match the model name.

首先,您必须将部分拖到文件夹~/共享/编辑器模板/并重命名它们以匹配模型名称。

Then you can call them in your view like this:

你可以这样称呼他们:

Html.EditorFor(model => model.ClientData)

Thanks to this your HTML code (the name attributes to be precise) will be generated in such a way that your default model binder will be able to bind this part of your view as well.

由于这一点,您的HTML代码(准确地说是名称属性)将以这样的方式生成,您的默认模型绑定器将能够绑定视图的这一部分。

#1


1  

Partials are usually not the best choice in case you want to place them inside one form and submit together. In such scenario it is better to take advantage of EditorTemplates which will solve your problem.

如果您想把它们放在一个表单中并一起提交,那么Partials通常不是最好的选择。在这种情况下,最好利用编辑器模板来解决您的问题。

Firstly you would have to drag your partials to the folder ~/Shared/EditorTemplates/ and rename them to match the model name.

首先,您必须将部分拖到文件夹~/共享/编辑器模板/并重命名它们以匹配模型名称。

Then you can call them in your view like this:

你可以这样称呼他们:

Html.EditorFor(model => model.ClientData)

Thanks to this your HTML code (the name attributes to be precise) will be generated in such a way that your default model binder will be able to bind this part of your view as well.

由于这一点,您的HTML代码(准确地说是名称属性)将以这样的方式生成,您的默认模型绑定器将能够绑定视图的这一部分。