Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Want to display associated name with an Id.

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
jkmlgn
Asked:
jkmlgn
  • 15
  • 10
2 Solutions
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
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
 
jkmlgnAuthor Commented:
Let's try this again to attach file.  Does the attached pdf provide the information you requested?
Mentorship2.pdf
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
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
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
Will let you know how it works out
0
 
jkmlgnAuthor Commented:
Produced an error.
Error.jpg
DataStructure.jpg
0
 
Barry62Commented:
@Html.DisplayFor(MentorName(modelItem =>item.MentorID))
0
 
jkmlgnAuthor Commented:
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
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
Error of MentorName does not exist in the current context.  Screenshot of error attached.
MentorName-Error.jpg
0
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
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
 
jkmlgnAuthor Commented:
I have attached a screen shot of the User Index View, perhaps this may be helpful.
UserIndex.jpg
0
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
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
 
Barry62Commented:
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
 
jkmlgnAuthor Commented:
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
 
jkmlgnAuthor Commented:
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
 
Barry62Commented:
OK, if you figure it out yourself can you give me an assist for my trouble?  Thanks!
0
 
jkmlgnAuthor Commented:
Absolutely!
0
 
jkmlgnAuthor Commented:
Here are the modified Model(database table) Classes.
Mentorship.pdf
0
 
Barry62Commented:
Glad I led you in the right direction.  Thanks.
0
 
jkmlgnAuthor Commented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 15
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now