Question

I am very new to asp.net mvc techniques, and have a question about how to render a view. In my project, I need to retrieve an XML file(XML string) from database. After getting this XML string, I deserialize xml to an object, say LogMessage, which I have defined already. Once I have got such a LogMessage object, I would like to display its contents to the user via a view. Additionally, I need to process some of the LogMessage properties before showing them to the user. For instance, (1) there is a logTime property in the LogMessage object, which is in utc format and I need to convert it to the local time, (2) there is a logCode property, which is in the format of int number (1,2,3, etc), and I need to convert each number to a meaningful name, such as eventStart, eventEnd, etc.

What in my mind now is that I create a strongly-typed view (of LogMessage type) in asp.net mvc3, so that I can render the view with Razor. Also I put all the necessary functions (e.g., for converting utc time to local time, mapping code number to its meaningful name, etc.) in the same view file, and call them when rendering the view.

But I am not sure whether I should do it as the aforementioned way or I should create another view model, say LogMessageViewModel (as I think actually the LogMessage is my data model or not ?). Then once I have got the LogMessage object, I can create a LogMessageViewModel (and LogMesageViewModel looks quite the same as LogMessage), and initialize the LogMessageViewModel with LogMessage and do all the necessary conversions in my Controller or Model, rather than do them in the View. Then now I have all the correct information in the LogMessageViewModel for the view, and create a strongly-typed view of LogMessageViewModel and simply render a view and show its contents to the user.

Could anyone give me some suggestions about these two different approaches or maybe there are some other better ones?

Was it helpful?

Solution

It is always a good practice to keep the DB layer and the UI layer seperate.

When you are getting data from the DB and storing it in a DataContract(LogMessage in your case), Use automappers to map it to a similar viewmodel(LogMessageViewModel ). Then use the viewModel in the views.

the flow goes like this

DB-> XML values -> LogMessage(DataContract/ Domain Object)-> using AutoMapper -> LogMessageViewModel-> View

OTHER TIPS

A Razor view engine that is used to render a Web Forms page to the response

Please go to here : ASP.NET MVC View Engine Comparison

That link may help your questions

and refer to : Rendering ASP.NET MVC Razor Views

It is always recommened to use ViewModels , if you require any more information that needs to rendered apart from the information from the model

If any additional processing needs to be done, you can do it in your view model.

Then, you can create a view strongly typed with the view model.

Hopw this helps..

If you are not using models, all the transformation logics , additional data needs to sent to view by using view data.

This makes controller fat.

It is a best practice to maintain your controllers as thin as possible.

Since, considering Single responsibilty principle . The responsibity of controllers is only to make transfer of controls.not any other thing else

**Conclusion: *Recommened to use ViewModel in your scenario***

Hope this helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top