Hi,
I have the following Views hierarchy:
_Layout.cshtml -> @model JobShop.Models.IndexVM -> is ok
|_ @{ Html.RenderPartial("_LeftSlider", Model.AspNetUsers); } -> nothing here -> is ok
|_ @RenderBody() for Home (Index) -> @model JobShop.Models.IndexVM -> is ok
|_ @{ Html.RenderPartial("_HeaderBanner");} -> @model JobShop.Models.IndexVM -> is ok Observation (A)
|_ @{ Html.RenderPartial("_MapCandidates", Model.Candidate); } -> @model IEnumerable<JobShop.Models.Candidate> -> is ok
|_ @{ Html.RenderPartial("_MapJobs", Model.Jobs); } -> @model IEnumerable<JobShop.Models.Jobs> -> is ok
|_ @{ Html.RenderPartial("_SliderCandidates", Model.Candidate); } -> @model IEnumerable<JobShop.Models.Candidate> -> is ok
|_ @{ Html.RenderPartial("_SliderJobs", Model.Jobs); } -> @model IEnumerable<JobShop.Models.Jobs> -> is ok
|_ @RenderBody() for IndexProfile -> @model JobShop.Models.IndexVM -> is ok
|_ @{ Html.RenderPartial("_MainProfile", Model.AspNetUsers); } -> nothing here -> is ok
|_ @{ Html.RenderPartial("_UserDetails", Model.CreditJournal); } -> nothing here -> is ok
|_ @RenderBody() for QuickProfile -> @model JobShop.Models.QuickProfileVM -> fail (1) Observation (B)
|_ @{ Html.RenderPartial("_MainProfile", Model.AspNetUsers); } -> nothing here -> N/A
|_ @{ Html.RenderPartial("_UserDetails", Model.CreditJournal); } -> nothing here -> N/A
for Jobs:
|_ @RenderBody() for Index -> @model PagedList.IPagedList<JobShop.Models.Jobs> -> fail (2) Observation (C1)
|_ @RenderBody() for Details -> @model JobShop.Models.Jobs -> N/A Observation (C2)
|_ @RenderBody() for New (Jobs) -> @model JobShop.Models.Jobs -> fail (3)
for Candidates:
|_ @RenderBody() for Index -> @model PagedList.IPagedList<JobShop.Models.Candidate> -> fail (4) Observation (D1)
|_ @RenderBody() for Details -> @model JobShop.Models.Candidate -> N/A Observation (D2)
|_ @RenderBody() for New (Candidates) -> @model JobShop.Models.Candidate -> fail (5)
The diagram above can be saw also
here on Dropbox
For all @model is about view considered in RenderBody or Renderpartial. New (Candidates) and New (Jobs) are views with EditorTemplates
Observation
(A) : (internally use both Model.Candidate and Model.Jobs)
(B) : (experimental since similar with above)
(C1 and C2) : Besides listing and display details, I want a data entry form for saving data in a related child table.
(D1 and D2) : Besides listing and display details, I want a data entry form for saving data in a related child table.
Failures:
1 -> The model item passed into the dictionary is of type 'JobShop.Models.QuickProfileVM', but this dictionary requires a model item of type 'JobShop.Models.IndexVM'.
2 -> The model item passed into the dictionary is of type 'PagedList.PagedList`1[JobShop.Models.Jobs]', but this dictionary requires a model item of type 'JobShop.Models.IndexVM'.
3 -> The model item passed into the dictionary is of type 'JobShop.Models.Jobs', but this dictionary requires a model item of type 'JobShop.Models.IndexVM'.
4 -> The model item passed into the dictionary is of type 'PagedList.PagedList`1[JobShop.Models.Candidate]', but this dictionary requires a model item of type 'JobShop.Models.IndexVM'.
5 -> The model item passed into the dictionary is of type 'JobShop.Models.Candidate', but this dictionary requires a model item of type 'JobShop.Models.IndexVM'.
N/A -> can't evaluate since parent view fails
The ViewModel is at that moment:
public class IndexVM
{
public IEnumerable<Jobs> Jobs { get; set; }
public IEnumerable<Candidate> Candidate { get; set; }
public IEnumerable<AspNetUsers> AspNetUsers { get; set; }
public IEnumerable<CreditJournal> CreditJournal { get; set; }
public IEnumerable<CandidateReview> CandidateReview { get; set; }
public IEnumerable<JobReview> JobReview { get; set; }
public List<Jobs> listJobs { get; set; }
public List<Candidate> listCandidate { get; set; }
public List<AspNetUsers> listAspNetUsers { get; set; }
public List<CreditJournal> listCreditJournal { get; set; }
public List<CandidateReview> listCandidateReview { get; set; }
public List<JobReview> listJobReview { get; set; }
}
and I use also:
public class UserModuleService
{
private JobShopEntities dbase = new JobShopEntities();
public List<Jobs> SelectJobs()
{
List<Jobs> Jobs = new List<Jobs>();
var jobs = dbase.Jobs.Include(j => j.AspNetUsers);
return jobs.ToList();
}
public List<Candidate> SelectCandidates()
{
List<Candidate> Candidate = new List<Candidate>();
var candidates = dbase.Candidate.Include(c => c.AspNetUsers);
return candidates.ToList();
}
public List<AspNetUsers> SelectUsers(string utilizator)
{
List<AspNetUsers> AspNetUsers = new List<AspNetUsers>();
var users = dbase.AspNetUsers.Include(u => u.AspNetRoles)
.Where(u => u.Id == utilizator);
return users.ToList();
}
public List<CreditJournal> SelectJournals(string utilizator)
{
List<CreditJournal> CreditJournal = new List<CreditJournal>();
var journals = dbase.CreditJournal.Include(cj => cj.AspNetUsers)
.Where(cj => cj.UseBy == utilizator);
return journals.ToList();
}
public List<CandidateReview> SelectCandidateReviews()
{
List<CandidateReview> CandidateReview = new List<CandidateReview>();
var candidatereviews = dbase.CandidateReview.Include(cr => cr.AspNetUsers);
return candidatereviews.ToList();
}
public List<JobReview> SelectJobReviews()
{
List<JobReview> JobReview = new List<JobReview>();
var jobreviews = dbase.JobReview.Include(jr => jr.AspNetUsers);
return jobreviews.ToList();
}
}
Sample of usage in Home Controller:
namespace JobShop.Controllers
{
public class HomeController : Controller
{
private readonly UserModuleService UserModuleService;
public HomeController()
{
this.UserModuleService = new UserModuleService();
}
public ActionResult FitToMarkersBounds(bool? fitToMarkersBounds)
{
ViewData["FitToMarkersBounds"] = fitToMarkersBounds ?? true;
return View();
}
public ActionResult Index()
{
var currentuser = User.Identity.GetUserId();
return View(new IndexVM
{
AspNetUsers = this.UserModuleService.SelectUsers(currentuser),
Jobs = this.UserModuleService.SelectJobs(),
Candidate = this.UserModuleService.SelectCandidates()
});
}
public PartialViewResult HeaderBanner()
{
return PartialView("_HeaderBanner", new IndexVM
{
Jobs = this.UserModuleService.SelectJobs(),
Candidate = this.UserModuleService.SelectCandidates(),
});
}
public PartialViewResult SelectCandidates()
{
var candidates = this.UserModuleService.SelectCandidates();
return PartialView("_SliderCandidates", candidates);
}
public PartialViewResult SelectJobs()
{
JobShopEntities db = new JobShopEntities();
var jobs = this.UserModuleService.SelectJobs();
if (User.Identity.IsAuthenticated)
{
var currentuser = User.Identity.GetUserId();
var TheUser = db.AspNetUsers.Where(u => u.Id == currentuser)
.Select(u => new
{
ID = u.Id,
Email = u.Email,
Username = u.UserName,
Surrname = u.Surname,
Name = u.Name,
Role = u.Role,
CreditBalance = u.CreditBalance
}).Single();
var idul = TheUser.ID;
var username = TheUser.Username;
var name = TheUser.Name;
var surrname = TheUser.Surrname;
var credit = TheUser.CreditBalance;
var role = TheUser.Role;
if (role == 3)
{
jobs.Where(jr => jr.User == currentuser)
.OrderBy(jr => jr.DateAdd);
}
else
{
jobs.OrderBy(jr => jr.DateAdd);
}
}
return PartialView("_SliderJobs", jobs);
}
public PartialViewResult MapCandidates()
{
var candidates = this.UserModuleService.SelectCandidates();
return PartialView("_MapCandidates", candidates);
}
public PartialViewResult MapJobs()
{
var jobs = this.UserModuleService.SelectJobs();
return PartialView("_MapJobs", jobs);
}
}
Sample of Action in Jobs Controller (for potentially saving a rating in list/detail view):
public class JobsController : Controller
{
private JobShopEntities db = new JobShopEntities();
[HttpGet]
public ActionResult RateJob()
{
var jobReview = new JobReview();
return View(jobReview);
}
[HttpPost]
public ActionResult RateJob(JobReview jobReview)
{
if (ModelState.IsValid)
{
db.JobReview.Add(jobReview);
db.SaveChanges();
return Redirect("/");
}
return View(jobReview);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
Ok, now the issue:
I want to find a way for proper deal with models, considering that if I render _LeftPartial it works this, but fails the views mentioned, if don't render (or don't provide model) all works (except _LeftPartial)
_LeftPartial is a kind of SidePanel that relies on _Layout (it must be available now matter of what is the current view if it is logged-in user). Here I display various information's about current user, provide user related navigation, and some settings)
For more detail, here is the JobsReview model:
public partial class JobReview
{
public int IdReview { get; set; }
public Nullable<int> JobID { get; set; }
public string Title { get; set; }
public string Review { get; set; }
public Nullable<int> Rating { get; set; }
public string ReviewedBy { get; set; }
public Nullable<System.DateTime> ReviewDate { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
public virtual Jobs Jobs { get; set; }
}
I want to find a way for dealing with RateJob ActionResult (in JobsController) from both Index and Details Views (in each I want to add an Form to perform the required Save)
As a detail all models are EF models.
Also is there a best practice in what regards IEnumerable vs. List since what I want about a certain model is to list, count, sort, filter and select a certain record (for Details and/or Edit Views)?