Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, i am trying to get the profile page of the current user but i am seeing an error on my screen and i don't know where it seems to be coming from. please, i need answers from any programmer that has a definite answer to this question. see below for details:

ERROR:
The model item passed into the dictionary is of type System.Linq.Enumerable+WhereSelectListIterator`2[Community.Models.Person.Profile,f__AnonymousType5`11[System.String,System.String,System.String,System.String,System.String,System.String,System.DateTime,System.String,System.Int32,System.Int32,System.Int32]], but this dictionary requires a model item of type System.Collections.Generic.IEnumerable`1[Community.Models.Person.Profile].


My code in profile controller:
[Authorize]
public ActionResult Proffe()
{
var profiles = (from d in db.Profiles.ToList()
where d.Username == User.Identity.Name
select new{
FirstName = d.FirstName,
LastName = d.LastName,
Address = d.Address,
Email = d.Email,
Phone = d.Phone,
Country = d.Country,
DateOfBirth = d.DateOfBirth,
Description = d.Description,
ProfileId = d.ProfileId,
Comment = d.Comments.Count,
Post = d.Posts.Count<i></i>
}).AsEnumerable();


return View(profiles);
}
Posted

1 solution

1) Create a strongly typed class

C#
public class ProfileEntity
   {
       public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
// add all the properties
   }


2) create a new list of the ProfileEntity class as

C#
var profiles = (from d in db.Profiles.ToList()
where d.Username == User.Identity.Name
select new ProfileEntity() { Address  = d.Address , FirstName =d.FirstName }).ToList(); // add all the properties


note: in the view, you have to use the model as ProfileEntity
 
Share this answer
 
Comments
[no name] 1-May-15 4:14am    
Thanks Man, this worked like magic... i have googled this like so many times now but only your solution seems to work for me. Thanks again because i have searched for this solution for like 3 months now. reason is because i am learning web application on my own and also learning c# together because i know vb not too well but i prefer to use it for windows form application because i use it in school.
Please, don't be offended.. i still have question to ask since you are more experienced than me..

its about my application.
Karthik_Mahalingam 1-May-15 4:17am    
if this solution helps you, please mark it as answer. else this post will be still open and other will be posting the answer by being considered as unanswered.
not at all offended, I am happy to help
[no name] 1-May-15 4:21am    
I want to create like a blog.. and i have three entities: post, comment and tags

The post has many comment, and many tags.
The Tag has many post.
Now the problem is i want it to appear like that of facebook but i will include some styling but that is the idea and i don't know how to code the controller. This is what i have so far but i used WebAPI controls which do not create a View for me and it is very difficult for me to create a view seeing that i am just new to web application and also c#.
I have videos, books and tutorials but no one target this actual problem.... PLEASE, help me out.

ENTITIES:

public class Post
{

public Post()
{
this.Comments = new HashSet<comment>();
this.Tags = new HashSet<tag>();
}
public int PostId { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public int PostedBy { get; set; }
public DateTime DatePosted { get; set; }
public virtual PostDate PostDates { get; set; }
public virtual Person.Profile Profile { get; set; }
public virtual ICollection<comment> Comments { get; set; }
public virtual ICollection<tag> Tags { get; set; }

}

public class Comment
{

public int CommentId { get; set; }
public int PostId { get; set; }
public int ProfileId { get; set; }
public string Message { get; set; }
public int CommentBy { get; set; }
public DateTime DateOfComment { get; set; }
public virtual Person.Profile Profile { get; set; }
public virtual Post Post { get; set; }
}

public class Tag
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Tag()
{
this.Posts = new HashSet<Post>();
}
public int TagId { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}


POST CONTROLLER:

public dynamic GetPosts()
{

var ret = (from post in db.Posts.ToList()
orderby post.DatePosted descending
select new
{
Title = post.Title,
Message = post.Message,
PostedBy = post.PostedBy,
PostedByName = post.Profile.User.UserName,
PostedByAvatar = "",
PostedDate = post.DatePosted,
PostId = post.PostId,
PostComments = from comment in post.Comments.ToList()
orderby comment.DateOfComment
select new
{
CommentBy = comment.CommentBy,
CommentedByName = comment.Profile.User.UserName,
CommentedByAvatar = "",
CommentedDate = comment.DateOfComment,
CommentId = comment.CommentId,
Message = comment.Message,
PostId = comment.PostId

}
}).AsEnumerable();
return ret;
}


// }

// GET api/Post/5
[ResponseType(typeof(Post))]
public async Task<ihttpactionresult> GetPost(int id)
{
Post post = await db.Posts.FindAsync(id);
if (post == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}

return Ok(post);
}

// PUT api/Post/5
public async Task
Karthik_Mahalingam 1-May-15 4:30am    
so, basically what you are trying to do ?
[no name] 1-May-15 5:37am    
I am trying to bring it up to a view so that it can be like a real post with a link to create a new post.
each post will have its comments and tags.
I don't know how to code this in controller and also send it to its view.
I used web API which has no scaffolding so there is no view for it.
I have been battling for this solution for like 2 months now.
Example: from my entity, i believe my view can be identical to that of Facebook and twitter only that it has a title in it.
Please, just a guidance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900