Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChris Gauthier
Courses Plus Student 6,434 PointsMVC / Model Implementation Question
I am a little confused on how to implement the array information on the characters array.
Can somebody help?
using System.Web.Mvc;
namespace Treehouse.Controllers.Models
{
public class VideoGamesController : Controller
{
public ActionResult Detail()
{
VideoGame vg = new VideoGame {
Title = "Super Mario 64";
Description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.";
}
ViewBag.Characters = new string[]
{
"Mario",
"Princess Peach",
"Bowser",
"Toad",
"Yoshi"
};
return View(vg);
}
}
}
@{
ViewBag.PageTitle = "Video Game Detail";
}
<h1>@ViewBag.Title</h1>
<h5>Description:</h5>
<div>@ViewBag.Description</div>
<h5>Characters:</h5>
<div>
<ul>
@foreach (var character in ViewBag.Characters)
{
<li>@character</li>
}
</ul>
</div>
namespace Treehouse.Models
{
// Don't make any changes to this class!
public class VideoGame
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string[] Characters { get; set; }
public string Publisher { get; set; }
public string DisplayText
{
get
{
return Title + " (" + Publisher + ")";
}
}
}
}
6 Answers
Chris Gauthier
Courses Plus Student 6,434 PointsAllright, that was it. Here is the working code for anyone that wants it:
using System.Web.Mvc;
using Treehouse.Models;
namespace Treehouse.Controllers
{
public class VideoGamesController : Controller
{
public ActionResult Detail()
{
var videogame = new VideoGame {
Title = "Super Mario 64",
Description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.",
Characters = new string[]
{
"Mario",
"Princess Peach",
"Bowser",
"Toad",
"Yoshi"
}
};
return View(videogame);
}
}
}
Steven Parker
231,275 PointsJust like you did with "Title" and "Description", you'll remove the "ViewBag." prefix and move it into the object literal that is initializing your VideoGame object. But there's a few other issues:
- the items inside the literal object should be separated by commas instead of semicolons
- the instructions said "Add a using directive for the
Treehouse.Models
namespace.", but it's still missing - the instructions did not say to change the namespace
Chris Gauthier
Courses Plus Student 6,434 PointsIm getting closer. But for some reason the problem with my using directive isn't what I need. Can you help ?
VideoGamesController.cs
using System.Web.Mvc;
using Treehouse.Models;
namespace Treehouse.Controller
{
public class VideoGamesController : Controller
{
public ActionResult Detail()
{
var videogame = new VideoGame(
Title = "Super Mario 64",
Description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.",
Characters = new Character[]
{
new Character { Name = "Mario"},
new Character { Name="Princess Peach"},
new Character { Name="Bowser"},
new Character { Name="Toad"},
new Character { Name="Yoshi"},
}
);
return View(videogame);
}
}
}
namespace Treehouse.Models
{
// Don't make any changes to this class!
public class VideoGame
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string[] Characters { get; set; }
public string Publisher { get; set; }
public string DisplayText
{
get
{
return Title + " (" + Publisher + ")";
}
}
}
}
Steven Parker
231,275 PointsYou're close, but the literal initialization object should follow the parentheses of the constructor call, not be inside them.
Also, the Characters should still be a string array. I don't think a Character class is defined anywhere, besides the VideoGame class definition says it's a string array.
Chris Gauthier
Courses Plus Student 6,434 PointsSo, do I have to implement the character array in the VideoGame Class?
Steven Parker
231,275 PointsThe actual property name is "Characters" (plural, capital "C") and you already have it in your code. But you have it as an array of "Character" objects (which have not been defined). It just needs to be a string array like it was in the code originally provided by the challenge.
Chris Gauthier
Courses Plus Student 6,434 Pointsdone, thanks again!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsHeh. But it's traditional to mark the answer the helped you reach the final solution as the "best answer".
Also, explicit answers without any explanation are strongly discouraged by Treehouse and may be subject to redaction by moderators.