Мне было интересно, может ли кто-нибудь помочь мне с построением моей модели представления ...
У меня есть три родительские модели: пользователи, профили и проекты
В представлении яхотел бы передать модель, которая содержит информацию о пользователе, информацию о его профиле и информацию о его проектах ...
Пока моя модель просмотра выглядит следующим образом ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProDevPortMVC3.ViewModels
{
public class PortfolioViewModel
{
public int ProfileID { get; set; }
public int UserID { get; set; }
public string ProfileFirstName { get; set; }
public string ProfileLastName { get; set; }
public string ProfileEmail { get; set; }
public Nullable<long> ProfileContactNo { get; set; }
public string ProfileCity { get; set; }
public string ProfileState { get; set; }
public string ProfileCountry { get; set; }
public string ProfilePhotoPath { get; set; }
public string ProfilePhotoName { get; set; }
public string ProfileBio { get; set; }
public string ProfileMissionStatement { get; set; }
public string ProfilePrivacySetting { get; set; }
public IEnumerable<ProfileBlog> ProfileBlogs { get; set; }
public IEnumerable<ProfileForum> ProfileForums { get; set; }
public IEnumerable<Project> Projects { get; set; }
}
}
проблема в том, что модель / таблица Projects имеет дочерние таблицы «один ко многим» ... и мне нужна вся их информация в модели представления ... Вот дочерние таблицы ...
ProjectCodeSamplesProjectDownloads ProjectScreenShots ProjectTechnologiesUseds
Вот мой метод действия контроллера до сих пор ...
public ActionResult Index(int id)
{
var profile = db.Profiles.SingleOrDefault(p => p.UserID == id);
var profileBlogs = db.ProfileBlogs.Where(p => p.ProfileID == profile.ProfileID);
var profileForums = db.ProfileForums.Where(p => p.ProfileID == profile.ProfileID);
var projects = db.Projects.Where(p => p.ProfileID == profile.ProfileID);
var viewModel = new PortfolioViewModel
{
ProfileFirstName = profile.ProfileFirstName,
ProfileLastName = profile.ProfileLastName,
ProfileEmail = profile.ProfileEmail,
ProfileContactNo = profile.ProfileContactNo,
ProfileCity = profile.ProfileCity,
ProfileState = profile.ProfileState,
ProfileCountry = profile.ProfileState,
ProfilePhotoPath = profile.ProfilePhotoPath,
ProfilePhotoName = profile.ProfilePhotoName,
ProfileBio = profile.ProfileBio,
ProfileMissionStatement = profile.ProfileMissionStatement,
ProfilePrivacySetting = profile.ProfilePrivacySetting,
ProfileBlogs = profileBlogs.Where(p => p.ProfileID == profile.ProfileID),
ProfileForums = profileForums.Where(p => p.ProfileID == profile.ProfileID),
Projects = projects.Where(p => p.ProfileID == profile.ProfileID)
};
return View(viewModel);
}
Итак, как мне продолжить или что мне делать отсюда?
ПРАВКАЯ забыл добавить, что у дочерних таблиц проекта есть внешний ключ ProjectID, который соединяет их с таблицей проектов ...
Вот окончательный код View для его работы ...
<code>@foreach (var item in Model.Projects)
{
<p>
@Html.DisplayFor(modelItem => item.ProjectName)
</p>
foreach (var subItem in item.ProjectDownloads)
{
<pre>
@Html.DisplayFor(modelItem => subItem.ProjectDownloadPath)
}}