Handling Creation and Edition Screen with EditorFor

Creation and Edition screen share a lot of similar information. The view is the same but change if it has a read only screen. Nevertheless, the create and edit view should share the same cshtml to limit future modification like adding a field or modifying existing fields.

It is possible to have the Creation.cshtml and Edition.cshtml use the same partial class. The code above shows how it can be done.

PartialViewForSharing

One way to do it is to use the html helper EditorForModel. Often, EditorFor and DisplayFor are used for individual input control but this can be used also for regrouping them. So, the EditorForModel is used to share between edition and creation the same group of edition control and inside the EditorTemplates you have a file name that has the model (or view model if you are using view model) used by the controller. Inside the editor templates of the model, you can use EditorFor to leverage the dynamic of editor template for controls.

EditorForForSharedModel

The form are not shared because they generate by the controller that have called them. For example, the edition one.

<form action="/House/Edit/1" method="post">

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.