What if I have or not the will to create a model that holds information from type x and y and a new type z.
So from your suggestion i get a custom type with some data from x and y. And to have a new custom type I should add the data I need for z.
And if I don't want to do that because data from z uses also the Id of the Display method to get an xml string containing data for a flash chart. I'll make bigger objects if I have to load for every custom type an extra z data en I will only use this data
type once on the view.
This is a situation where you have a list of statistics and a chart showing you the data.
With your comment --> but you are asking for bugs.
Do you mean this ViewData is still bugged ? Or this will create buggs ?
<div>
It is good practice to avoid using ViewData at all, because the form that it used:
magic strings with need to unboxing them, and to check for null value before unboxing types that aren't nullable's.
Using ViewModel as a place to put all what the view is using is a good practice.
Is it ok to keep some special data in server session vars ?
I'm testing a login form and I could just use the autentication methods of .net but as I currently use some session vars to check if a user is of a type I'll try first that approach.
So is it ok to do in mvc : Session["isAdmin"] = true;
It is good practice to avoid using ViewData at all
I had read about three books about MVC, lot of articles which are always againt ViewData.
But I am not always against ViewData.
During Devlopment pages are rapidly changes thier layout.
Every time page layout changes(for example new data needs to show here or remove contents from the page) you change your View Model(not Domain Model).
Let say you have a View View1 which had four partials which all are strongly typed of A, B ,C, D. You just creted
View1ViewModel which had all data's for A,B,C,D partials.
If one partial is removed or added you need to change your View1ViewModel
If you use seprate ViewData for every partial then you just need to just pass the ViewData in your all views that that uses this partial.
In my current devlopment i found myself ViewData is comfortable then ViewModel because of sharing partial and rapid change in layout
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
When you use View() method it will return ViewResult which contains ViewData.
Check
http://forums.asp.net/t/1351459.aspx
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
charly.p
Member
348 Points
305 Posts
MVC first time with composite classes
Jun 14, 2010 09:41 AM|LINK
Ok I googled further and found indeed some good resources to start with MVC and Data FrameWorks.
Now I face another porblem, I have a controller that would like to pass a composite object to a view, having that view of type X.
With composite type I mean I'm selecting with a linq query data from type X and data from type Y.
How can I pass this to my view ?
I try this in the case where I have a gridview to display with data from a select with some joins. And so doing this scenario in mvc style.
I already got this answer from another thread of mine :
From ignatandrei :
Composition is the simplest answer.Just make a class that has 2 properties , X and Y
public class Composite
{
public X x{get;set;}
public Y y{get;set;}
}
but please open a new thread for a new question
ignatandrei
All-Star
134903 Points
21619 Posts
Moderator
MVP
Re: MVC first time with composite classes
Jun 14, 2010 10:31 AM|LINK
Inherits="System.Web.Mvc.ViewUserControl<Composite>"
or
Inherits="System.Web.Mvc.ViewPage<Composite>"
See "Passing Strongly-Typed Data Between a Controller and a View"
http://msdn.microsoft.com/en-us/library/dd394711.aspx
Also, after reading this, look at Html.DisplayFor with opinionated templates
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
charly.p
Member
348 Points
305 Posts
Re: MVC first time with composite classes
Jun 14, 2010 11:21 AM|LINK
Clear nice articles,
What if I have or not the will to create a model that holds information from type x and y and a new type z.
So from your suggestion i get a custom type with some data from x and y. And to have a new custom type I should add the data I need for z.
And if I don't want to do that because data from z uses also the Id of the Display method to get an xml string containing data for a flash chart. I'll make bigger objects if I have to load for every custom type an extra z data en I will only use this data type once on the view.
This is a situation where you have a list of statistics and a chart showing you the data.
ignatandrei
All-Star
134903 Points
21619 Posts
Moderator
MVP
Re: MVC first time with composite classes
Jun 14, 2010 11:29 AM|LINK
Put into ViewData - but you are asking for bugs
What's the problem ? With RenderPartial you send your model through another view
See also
http://blogs.intesoft.net/post/2009/02/renderaction-versus-renderpartial-aspnet-mvc.aspx
charly.p
Member
348 Points
305 Posts
Re: MVC first time with composite classes
Jun 14, 2010 11:46 AM|LINK
With your comment --> but you are asking for bugs.
Do you mean this ViewData is still bugged ? Or this will create buggs ?
I'll check the new link you gave me, these are all new methods that I didn't know about.
krokonoster
Contributor
4291 Points
1352 Posts
Re: MVC first time with composite classes
Jun 14, 2010 11:50 AM|LINK
It is good practice to avoid using ViewData at all, because the form that it used: magic strings with need to unboxing them, and to check for null value before unboxing types that aren't nullable's.
Using ViewModel as a place to put all what the view is using is a good practice.
</div>charly.p
Member
348 Points
305 Posts
Re: MVC first time with composite classes
Jun 14, 2010 01:29 PM|LINK
Is it ok to keep some special data in server session vars ?
I'm testing a login form and I could just use the autentication methods of .net but as I currently use some session vars to check if a user is of a type I'll try first that approach.
So is it ok to do in mvc :
Session["isAdmin"] = true;
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: MVC first time with composite classes
Jun 14, 2010 03:59 PM|LINK
I had read about three books about MVC, lot of articles which are always againt ViewData.
But I am not always against ViewData.
During Devlopment pages are rapidly changes thier layout.
Every time page layout changes(for example new data needs to show here or remove contents from the page) you change your View Model(not Domain Model).
Let say you have a View View1 which had four partials which all are strongly typed of A, B ,C, D. You just creted View1ViewModel which had all data's for A,B,C,D partials.
If one partial is removed or added you need to change your View1ViewModel
If you use seprate ViewData for every partial then you just need to just pass the ViewData in your all views that that uses this partial.
In my current devlopment i found myself ViewData is comfortable then ViewModel because of sharing partial and rapid change in layout
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ignatandrei
All-Star
134903 Points
21619 Posts
Moderator
MVP
Re: MVC first time with composite classes
Jun 14, 2010 04:12 PM|LINK
And for testability ?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: MVC first time with composite classes
Jun 14, 2010 05:32 PM|LINK
ViewData is also easily testable
When you use View() method it will return ViewResult which contains ViewData.
Check
http://forums.asp.net/t/1351459.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD