views:

101

answers:

3

I'm using NerdDinner as a guide for my first MVC/LINQ to SQL project. It discusses the use of the ViewModel pattern when a View needs data from multiple sources - in their example: Dinners and Countries (serves as the drop down list).

In my application, the problem is a bit different. It's not so much different data, rather data linked via a key constraint. I have a Story table that links to aspnet_users via the UserId key. I would like to have easy access to the UserName for each story.

Since I'm using the repository pattern and returning IQueryable in some cases for deferred execution, I'm struggling with the correct way to shape the data. So I'm using this VideModel pattern right now to make it work but not sure if it's the right way.

Instead of returing IQueryable Story (which wouldn't work since I need the UserName), I'm returning a new custom class UserStory, which has a Story property and a string Username property.

What are your thoughts?

A: 

It seems like your question has less to do with MVC as it is simply a question about how to access the story data based on the username string.

Would it be possible to create a view in your database with all the UserStory data, the username, along with userid in it? That way, you could select from the view based on the username you have.

To create the view, you would simply have to do a join between the user table and the userstory table based on the userid.

After that, you could still use the repository pattern with the IQueryable being returned.

If you are wanting to do updates, it would be simple to do since you still have the userid, and would be able to link back to the actual table which would need the update.

cspritch76
I get tripped up because originally I was returning an IQueryable of Story but pulling the username no longer makes that a Story and I was getting anonymous to story casting errors.
Beavis
A: 

Using the repository pattern and returning an IQueryable of Stories is fine. The relationship allows you to access the the username value something like this >>

Assuming you are returning the IQueryable in your model object:

foreach(Story story in Model.Stories)
{
    // do something with the value
    response.write(story.aspnet_user.UserName);
};

Your Repository method would look like this:

public List<Stories> GetStories(Guid UserId)
{
    return datacontext.Stories.Where(u => u.UserId = UserId).ToList();
}

The relationship will automatically provide you with access to the UserName value in the foreach loop i first mentioned. nothing more is required.

I'm not sure why your pagination control failed on Count() though??

Hope this helps

Mark
The problem stems from a pagination control I found online that I'm using. When I was just returning IQueryable of Stories (minus the Username) it was just fine. However, after trying your method the application bombed at runtime when the pagination helper did a source.Count(); I don't fully understand anonymous types/linq to know why it did that. However, I avoided this error by creating the UserStory class and returing IQueryable UserStory...any ideas why this happened?
Beavis
A: 

If you look at Kigg, you will see that they mess about with the initial model to create custom ViewModels. That's the thing that NerdDinner doesn't cover in any detail. You could create a StoriesWithUserName class that inherits from Stories, but adds a new property - UserName. Then you return that to your View which would inherit from IEnumerable<StoriesWithUserName>

[EDIT]

Oops. Didn't spot that you already did this :o)

MikeB