1

I have been given the task of creating a template for Views in MVC. Basically, instead of many Views (CRUD) for each entity, we would only have one that accepts generic model and displays it in Edit/Display mode.

I have so far played around with IView, WebViewPage and ViewPage, but I can't seem to get anything to work. I also searched for something like this, but can't find anything useful really.

Specifically, I don't know which C# class I could overwrite/implement to get my desired effect. Can anybody help me out here?

LeonidasFett
  • 3,052
  • 4
  • 46
  • 76

1 Answers1

-1

When using MVC you normally have Razor Views (.cshtml). These come in 4 flavours:

  • Full Views
  • Partial Views
  • EditorTemplates
  • DisplayTemplates

From your question, I think, you want EditorTemplates.

Create a new View in Views/Shared/EditorTemplates and name it YourGenericModel.cshtml Inside this file write the first line:

@model YourGenericModel

You can now specify what should be rendered using the normal Razor syntax.

To Have your model displayed using your new View simply call

@Html.EditorFor(model => model.yourGenericModelInstance)
wertzui
  • 5,148
  • 3
  • 31
  • 51
  • that is default view. I am looking for creating views for generic models. something where I could use reflection for resovling properties and then adding controls to the view dynamically and send it to the client. something like that. – LeonidasFett Jan 26 '16 at 15:58
  • Inside the YourGenericModel.cshtml you can call @Html.Editor(property, "someNameFromReflection") for each property. Use .GetType().GetProperties()... to iterate through all properties. – wertzui Jan 26 '16 at 16:19
  • ok maybe I understood you wrong. what I don't understand, how can I use "model.yourGenericModelInstance" when using generics? and how would I write my @model declaration when passing a generic model which requires a parameter? – LeonidasFett Jan 26 '16 at 17:00