简体   繁体   English

ASP.NET MVC - 编辑和只读的单一视图?

[英]ASP.NET MVC - Single view for Edit and Read only?

We have a situation where we need a read only version of an edit page. 我们需要一个只读版本的编辑页面。 This is permission based as well as based on the status of the object. 这是基于权限以及基于对象的状态。 My thought is to separate this out into a completely new view but the other opinions are to place this logic in the edit view. 我的想法是把它分成一个全新的视图,但其他意见是将这个逻辑放在编辑视图中。

Some information about how the view is structured: 有关视图结构的一些信息:

Form elements 表单元素

Editor templates 编辑模板

Partial views 部分观点

Kendo controls (fluent wrappers) 剑道控制(流畅的包装)

So in order to complete the task at hand means the read only functionality needs to exist in multiple places and must be passed to editor templates / partial views by view data. 因此,为了完成手头的任务,意味着只读功能需要存在于多个位置,并且必须通过视图数据传递给编辑器模板/部分视图。

There is also HTML helpers / extensions that are being used on the page which means updating these too. 还有在页面上使用的HTML帮助/扩展,这意味着也要更新它们。

Lastly, with the Kendo controls, a lot of the logic of the grid (in-line editing functionality) logic exists in external JS files so we have yet another place to modify the code. 最后,使用Kendo控件,外部JS文件中存在很多网格逻辑(内联编辑功能)逻辑,因此我们还有另一个地方可以修改代码。

My question is what is the 'best practice' in this regard? 我的问题是这方面的“最佳做法”是什么?

Do we duplicate mark-up (cshtml) by separating the read only view from the edit view. 我们是否通过将只读视图与编辑视图分开来复制标记(cshtml)。

Or do we change the edit view to accommodate the read only functionality? 或者我们是否更改编辑视图以适应只读功能?

It just seems like changing the edit view will add a whole lot of complexity and dependencies for such a trivial task. 看起来改变编辑视图似乎会为这样一个简单的任务增加很多复杂性和依赖性。

You can share the single view for readonly and editable elements by using razor conditioning in the view, But it seems to complicated to manage if the view content complex processing or elements , I recommend you to use separate view and send the appropriate view from the controller's action method depends on your criteria. 您可以通过在视图中使用razor条件来共享单个视图以获取只读和可编辑元素。但是, 如果视图内容复杂处理或元素管理似乎很复杂 ,我建议您使用单独的视图并从控制器发送相应的视图行动方法取决于您的标准。

You can also use Html.EditorForModel directly to render elements as per your model data annotations OR Html.DisplayForModel to render all model properties for read only purpose. 您还可以直接使用Html.EditorForModel根据模型数据注释或Html.DisplayForModel呈现元素,以呈现所有模型属性以供只读使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM