Solved

Want to display associated name with an Id.

Posted on 2013-01-29
25
209 Views
Last Modified: 2013-03-18
I have a problem displaying the desired data instead of the default data built by mvc on an index page.  I have a User class, a Mentorship class, and a Project class.  The trick is that the User table contains all of the users, both users and mentors, as seen in the Mentorship class.  In this case the MentorID is simply the UserID.  Yes it would be alot easier to use two tables, however, because the user data is imported from an external data source I am told that using two tables will not be permitted.  When I display the Mentorship Index page I need to display an actual name in both the UserID column and the MentorID column.  The UserID part has been fairly easy to accomplish, displaying the FullName based on associated ID for the MentorID has been elusive.  How do I accomplish this?
Mentor.pdf
0
Comment
Question by:jkmlgn
  • 15
  • 10
25 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 38831071
So you want your page to look like the pdf you attached, or is that what it is now and you need it to be different?

Please give us the code for your classes so we can better help you with this.
0
 

Author Comment

by:jkmlgn
ID: 38831282
That is what I have now, I wish for the Mentor column to display a full name and not an Id number.  The models, controller, and view are attached as a pdf.
0
 

Author Comment

by:jkmlgn
ID: 38831823
Let's try this again to attach file.  Does the attached pdf provide the information you requested?
Mentorship2.pdf
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Expert Comment

by:Barry62
ID: 38832612
I may be wrong, but I would say, without seeing your data structure, that you need to replace

@Html.DisplayFor(modelItem => item.MentorID)

with

@Html.DisplayFor(modelItem => item.mentorship.MentorID.fullName)
0
 

Author Comment

by:jkmlgn
ID: 38832630
If by data structure you are refering to the Model classes they were included in the attached file.  Were you unable to view the pdf attachment?
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38832651
No, I was able to see it, but what I meant was your table structures - i.e. Users and Mentorships.

Please try my suggestion.  I think it will work.
0
 

Author Comment

by:jkmlgn
ID: 38832668
Will let you know how it works out
0
 

Author Comment

by:jkmlgn
ID: 38832697
Produced an error.
Error.jpg
DataStructure.jpg
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38832727
@Html.DisplayFor(MentorName(modelItem =>item.MentorID))
0
 

Author Comment

by:jkmlgn
ID: 38832779
That generated a similar error, displayed in attachment.

In the Mentorship Index View, the second column item displays as an integer, MentorID, what I would like to display is the FullName that is associated with that MentorID.  The MentorID in the Mentorship Table has a corresponding UserID in the User Table such that Mentorship.MentorID = User.UserID.  How can I display the FullName from the User Table that corresponds to the MentorID instead of the MentorID?  I am new to C# and MVC so it is possible that my MentorshipConfiguration is either incorrect and/or incomplete to properly represent this unique relationship.  Getting the FullName associated with the UserID to display in column 1 was an easy task, this one has been an exercise in frustration so far.  Thank you for your time and assistance.
Error2.jpg
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38832803
Check my last comment.  I edited it apparently after you looked at it.  I had the MentorName function inside the modelItem, which is wrong.  It now shows the entire modeItem instance of MentorID inside the MentorName function.  this should pass the id to your function and return the name.
0
 

Author Comment

by:jkmlgn
ID: 38832825
Error of MentorName does not exist in the current context.  Screenshot of error attached.
MentorName-Error.jpg
0
 
LVL 8

Assisted Solution

by:Barry62
Barry62 earned 400 total points
ID: 38832911
You have a User defined as a parameter in you Mentorship model, so you should be able to call up the Fullname string from the User associated with the Mentor.


@Html.DisplayFor(modelItem =>item.MentorID.User.FullName)
0
 

Author Comment

by:jkmlgn
ID: 38832947
That's what I thought, because of the navigation this should be a simple solution, however looks are apparently deceiving.  I have been dancing around this for two weeks and here I am.  It is proving to be an elusive solution.  Error file attached.
UserError.jpg
0
 

Author Comment

by:jkmlgn
ID: 38832959
I have attached a screen shot of the User Index View, perhaps this may be helpful.
UserIndex.jpg
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38833004
Try taking out the .MentorID before .User

User is already associated with a MentorID for each modelItem, isn't it?



@Html.DisplayFor(modelItem =>item.User.FullName)
0
 

Author Comment

by:jkmlgn
ID: 38833068
In regards to Mentorship yes, a user is not required to have a Mentor, a user can have multiple Mentors, a Mentor must have a User.  The Mentorship relationship involves two users from the User Table, one that will be Mentorship.UserID and another one that will be Mentorship.MentorID. Only a student can be Mentorship.UserID and only a Faculty can be Mentorship.MentorID.  While a Faculty has not entered into a Mentorship relationship they remain a User and not a User/Mentor.  Your previous post does indeed produce the FullName of the User that is the Mentorship.UserID portion of the relationship, that much I was able to produce on my own.  Now, getting the FullName of the User which is the Mentorship.MentorID that is a trick.  Note that the Mentorship.MentorID will be identical to an existing Faculty UserID, which of course means that the Mentorship.UserID will be identical to an existing Student UserID.  Please not the subtle differences between the User Index and the Mentorship Index screen shots.  The Students are the User in the Mentorship View, the displayed Integer under the Mentor heading are the Mentorship.MentorID, this is also the User.UserID of a Faculty User.  While I know this is an odd data model, when a new user logs in their profile is 'imported' from an external data source, it is because of this data source that all users must remain in one table and has created this scenario.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38833641
OK, as you can probably tell, I am not a C# expert, but I do know Java, so I am trying to translate things in my head as I go.  Here is an idea:

Could you call for the Fullname of the User using the ID of the mentor like this:

@Html.DisplayFor(modelItem =>item.User(modelItem=>item.MentorID).FullName)
0
 

Author Comment

by:jkmlgn
ID: 38835108
Unfortunately neither am I, most of my experience has been process control programming with a little bit of VB, VB.Net, and occasional SQL.  I have tried to go at this utilizing a SQL WHERE statement but to no avail.  Attached is the current error, cannot use as a method.
User-Method-Error.jpg
0
 

Author Comment

by:jkmlgn
ID: 38837231
I have found a major flaw in the database model, am currently attempting to re-design the data model so for now this question is a moot issue.  Thank you for your time and assistance.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38837252
OK, if you figure it out yourself can you give me an assist for my trouble?  Thanks!
0
 

Author Comment

by:jkmlgn
ID: 38837274
Absolutely!
0
 

Accepted Solution

by:
jkmlgn earned 0 total points
ID: 38852747
Here are the modified Model(database table) Classes.
Mentorship.pdf
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38852903
Glad I led you in the right direction.  Thanks.
0
 

Author Closing Comment

by:jkmlgn
ID: 38870840
Thanks for your time and assistance Barry62, reviewing our conversation led me to make the changes in the model that allowed for the displaying of the name and not just the Id.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question