You're still doing the viewmodel incorrectly. See my last post, copy and paste it into your viewmodel.
And for the underlined "Director", see my other last page on page 1. It's "directors", not "Directors". Programming is case-sensitive when it comes to most variables.
Ah sorry didnt see you post regarding the view model.
I edited the code and perfect no errors.
But when I run the project and click on "Store" I get the following error:
Server Error in '/' Application.
Compilation Error
Description:An
error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message:CS0246: The type or namespace name 'ViewModels' could not be found (are you missing a using directive or an
assembly reference?)
Source Error:
Line 27:
Line 28:
Line 29: public class _Page_Views_Store_Index_cshtml : System.Web.Mvc.WebViewPage<ViewModels.IndexViewModel > {
When
I go back to my project two errors appear. One the same as the above error and the other is:
Error 1 The
type or namespace name 'ViewModels' could not be found (are you missing a using directive or an assembly reference?)
Error 2 foreach
statement cannot operate on variables of type 'ViewModels.IndexViewModel' because 'ViewModels.IndexViewModel' does not contain a public definition for 'GetEnumerator'
Below
is the code for the Store index page:
@model ViewModels.IndexViewModel
@{ViewBag.Title ="Store";}
<p>Select
from @Model.Count() Genre's:</p>
<ul>
@foreach
(var
genre in
Model)
{
<li>
@Html.ActionLink(genre.Name,"Browse",
new
{ genre = genre.Name })
</li>
}
</ul>
<h3>Browse
Directors</h3>
<p>
Select from @Model.Directors.Count() genres:
</p>
<ul>
@foreach
(var
director inModel.Directors)
{ <li>@director.Name</li>
}
</ul>
The
two parts in italics and underlined are the errors.
The viewmodel is now ok but I am still getting some errors in the controller:
Error 1 'Component2.ViewModels.IndexViewModel' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'Component2.ViewModels.IndexViewModel' could be found (are you missing a using directive or an assembly
reference?)
Error 2 foreach statement cannot operate on variables of type 'Component2.ViewModels.IndexViewModel' because 'Component2.ViewModels.IndexViewModel' does not contain a public definition for 'GetEnumerator'
Wow thank you very much for that mate. Works fine.
I've made the director names into action links so they work in the same way that genre links work.
Am I right in think that I need to add code into the store controller under the browse part. The same way it has for the genres. Or do I need to edit it completely?
To get it up and running, I would suggest creating 2 new actionresults in your controller.
Call one "BrowseGenres" and the other "BrowseDirectors".
Just take the code from "Browse" and stick it into BrowseGenres.
We would do this because your View currently has no way to tell the difference between genres and directors, so having each point to their own actionresult is eaiest.
Be sure to makre any posts as the answer if you are satisfied.
However when I click on the directors name I get an error. This links to the browse view which has nothing to do with the director. Please could you
explain what need to add?
Below is the code I have inserted into
the controller:
JohnLocke
Contributor
3008 Points
686 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 05:29 PM|LINK
THIS is what your viewmodel should look like....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Component2.Models;
public class IndexViewModel {
public List<Genre> Genres { get; set; }
public List<Artist> Directors { get; set; }
public IndexViewModel(List<Genre> _genres, List<Director> _directors) {
Genres = _genres;
Directors = _directors;
}
}
uzzy
0 Points
116 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 05:40 PM|LINK
I have edited the IndexViewModel to this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Component2.Models;
public class IndexViewModel
{
public List<Genre> Genres { get; set; }
public List<Director> Directors { get; set; }
}
And now there are no errors.
However, I am still getting an error on the controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Component2.ViewModels;
using Component2.Models;
namespace Component2.Controllers
{
// // GET: /Store/
public class StoreController : Controller
{
MovieStoreEntities storeDB = new MovieStoreEntities();
public ActionResult Index()
{
var genres = new List<Genre>
{
new Genre { Name = "Horror"},
new Genre { Name = "Comedy"},
new Genre { Name = "Action"}
};
var directors = new List<Director>
{
new Director { Name = "Michael Bay"},
new Director { Name = "Franco Zeffirelli"},
new Director { Name = "Jay Roach"}
};
var vModel = new IndexViewModel (genres, Directors);
return View(vModel);
}
The bit in "Directors" in italics and underlined is where the error is:
Error 1 The name 'Directors' does not exist in the current context
I don't understand this error as I thought I had declared the model at the top and declared it in the viewmodel. Or am I completely missing something?
JohnLocke
Contributor
3008 Points
686 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 05:42 PM|LINK
You're still doing the viewmodel incorrectly. See my last post, copy and paste it into your viewmodel.
And for the underlined "Director", see my other last page on page 1. It's "directors", not "Directors". Programming is case-sensitive when it comes to most variables.
uzzy
0 Points
116 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 05:58 PM|LINK
Ah sorry didnt see you post regarding the view model.
I edited the code and perfect no errors.
But when I run the project and click on "Store" I get the following error:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'ViewModels' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 27: Line 28: Line 29: public class _Page_Views_Store_Index_cshtml : System.Web.Mvc.WebViewPage<ViewModels.IndexViewModel > {When I go back to my project two errors appear. One the same as the above error and the other is:
Error 1 The type or namespace name 'ViewModels' could not be found (are you missing a using directive or an assembly reference?)
Error 2 foreach statement cannot operate on variables of type 'ViewModels.IndexViewModel' because 'ViewModels.IndexViewModel' does not contain a public definition for 'GetEnumerator'
Below is the code for the Store index page:
@model ViewModels.IndexViewModel
@{ViewBag.Title ="Store";}
<p>Select from @Model.Count() Genre's:</p>
<ul>
@foreach (var genre in Model)
{
<li>
@Html.ActionLink(genre.Name,"Browse", new { genre = genre.Name })
</li>
}
</ul>
<h3>Browse Directors</h3>
<p>
Select from @Model.Directors.Count() genres:
</p>
<ul>
@foreach (var director inModel.Directors)
{
<li>@director.Name</li>
}
</ul>
The two parts in italics and underlined are the errors.
JohnLocke
Contributor
3008 Points
686 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:07 PM|LINK
Sorry, you got that error because we didn't include the namespace in your viewmodel. Below is what your viewmodel should finally look like.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Component2.Models;
namespace Component2.ViewModels {
public class IndexViewModel {
public List<Genre> Genres { get; set; }
public List<Artist> Directors { get; set; }
public IndexViewModel(List<Genre> _genres, List<Director> _directors) {
Genres = _genres;
Directors = _directors;
}
}
}
Lastly, @model ViewModels.IndexViewModel should be @model Component2.ViewModels.IndexViewModel
I should start copying and pasting code from VS instead of going off memory :P
uzzy
0 Points
116 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:17 PM|LINK
I really appreciate all this mate.
The viewmodel is now ok but I am still getting some errors in the controller:
Error 1 'Component2.ViewModels.IndexViewModel' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'Component2.ViewModels.IndexViewModel' could be found (are you missing a using directive or an assembly reference?)
Error 2 foreach statement cannot operate on variables of type 'Component2.ViewModels.IndexViewModel' because 'Component2.ViewModels.IndexViewModel' does not contain a public definition for 'GetEnumerator'
Below is the code:
@model Component2.ViewModels.IndexViewModel
@{
ViewBag.Title ="Store";
}
<p>
Select from @Model.Count() Genre's:
</p>
<ul>
@foreach (var genre in Model)
JohnLocke
Contributor
3008 Points
686 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:23 PM|LINK
When you use a ViewModel, you reference it by using @Model.VariableName
So for "Select from @Model.Count() Genre's:"
it's actually
Select from @Model.Genres.Count() Genre's:
and
for "@foreach (var genre in Model)"
it's actually
@foreach (var genre in Model.Genres)
uzzy
0 Points
116 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:32 PM|LINK
Ah yes I understand.
Wow thank you very much for that mate. Works fine.
I've made the director names into action links so they work in the same way that genre links work.
Am I right in think that I need to add code into the store controller under the browse part. The same way it has for the genres. Or do I need to edit it completely?
JohnLocke
Contributor
3008 Points
686 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:39 PM|LINK
To get it up and running, I would suggest creating 2 new actionresults in your controller.
Call one "BrowseGenres" and the other "BrowseDirectors".
Just take the code from "Browse" and stick it into BrowseGenres.
We would do this because your View currently has no way to tell the difference between genres and directors, so having each point to their own actionresult is eaiest.
Be sure to makre any posts as the answer if you are satisfied.
uzzy
0 Points
116 Posts
Re: Music Store Tutorial: Store View
Apr 06, 2012 06:52 PM|LINK
However when I click on the directors name I get an error. This links to the browse view which has nothing to do with the director. Please could you explain what need to add?
Below is the code I have inserted into the controller:
// // GET: /Store/Browse?genre=Horror
public ActionResult Browsegenre (string genre)
{
// Retrieve Genre and its Associated Movies from database
var genreModel = storeDB.Genres.Include("Movies")
.Single(g => g.Name == genre);
returnView(genreModel);
}
public ActionResult browsedirector (string director)
{
// Retrieve Director and its Associated Movies from database
var directorModel = storeDB.Directors.Include("Movies")
.Single(g => g.Name == director);
return View(directorModel);
}
Below is the browse view code:
@model Component2.Models. Genre
@{ViewBag.Title = "Browse Movies";
}
<div class="genre">
<h3><em>@Model.Name</em> Movies</h3>
<ul id="movie-list">
@ foreach (var movie in Model.Movies)
{
<li>
<a href="@Url.Action("Details" ,new { id = movie.MovieId })">
<img alt="@movie.Title" height="120px" width ="100px" src="@movie.MovieArtUrl" />
<span>@movie.Title</span>
</a>
</li>
}
</ul>
</div>