1

I have number of models say : A1, A2 A3 & A4.

There will be views for each of these models (like List or Create , Edit etc.,.)

However my views should not have any static html in it, I have to render the html by using some Html Helper methods.

Probably I should write a method in RenderHelper.cs like below

public static MvcHtmlString RenderContent(this HtmlHelper helper, IEnumerable<FatRoute> model)
    {
    }

also, I have to check the type of column in the model to render textbox or dropdown or datetime according to its type.

The problem here is, the columns are diff foreach model (obviously) and I cannot have diff blocks of code to check the column types for each model. If I say item. (here I get the columns in the item) in foreach bolck, I get only the columns of the passed model. But it should be some generic way to handle all models.

can somebody advise how to achiev this?

2 Answers 2

2

MVC already provides a way to do this, more or less. They're called Display/Editor Templates. They already use reflection to render your models dynamically. If you don't like the way they do it by default, you can simply replace it with your own custom object template.

You can render your entire model simply by using @Html.EditorForModel or @Html.DisplayForModel

You can read about how this works here:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the info. Im going through the article. Just one thought, it is the feature in mVC 2.However we are now in MVC4, is there any better way to do this in MVC4 and what are the pros and cons of using default templates?
@mmssddff - MVC3 and MVC4 are essentially a superset of MVC2 (there are some changes, but by and large a superset). This hasn't changed. There are no pros and cons, either you like the way the templates render or you don't, if you don't, you use your own. You can download the default templates from the link in the article and modify them to suit your needs.
@mmssddff - hmm.. doesn't seem to be a link anymore.. but the default templates are in the MVCFutures source, which you can find here aspnetwebstack.codeplex.com/SourceControl/latest#src/…
The article talks about .aspx and .ascx pages.however im using .cshtml pages (views) in MVC4. can you advise how to override template files for views ? Also I am using Razor views, or pls share any site that have the info.
@mmssddff - You can use aspx/ascx template files with Razor files, no problem. If you insist on making them razor, then it's ridiculously trivial to change <% %> to @
0

Since you have decided to use a custom helper, one way is to use reflection to iterate through the properties of the model and use that knowledge to build the views.

This way, you may use features such as property annotations (data annotations) such as DisplayNameAttribute on properties of the model to supply additional data to the view generator.

With this approach, you will be targeting each type of property (column) defined on the model and not the model itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.