Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

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.

like image 521
Mikechats Avatar asked Jul 09 '14 04:07

Mikechats


1 Answers

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.

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.

like image 195
Hiren Kagrana Avatar answered Oct 06 '22 06:10

Hiren Kagrana