using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web.Mvc; using System.Web.Mvc.Html; using Portoa.Web.Util; using VideoGameQuotes.Api; namespace VideoGameQuotes.Web.Models { public class QuoteSubmitModel { [Required, DisplayName("Quote")] public string QuoteText { get; set; } public int GameId { get; set; } //if a new game is created [DisplayName("Name")] public string GameName { get; set; } [DisplayName("Website")] public string GameWebsite { get; set; } [DisplayName("Region")] public IList GameRegions { get; set; } [DisplayName("Publishers")] public IList PublisherIds { get; set; } [DisplayName("Systems")] public IList SystemIds { get; set; } //if a new publisher is created [DisplayName("Name")] public string PublisherName { get; set; } [DisplayName("Website")] public string PublisherWebsite { get; set; } //if a new system is created [DisplayName("Name")] public string SystemName { get; set; } [DisplayName("Abbreviation")] public string SystemAbbreviation { get; set; } [DisplayName("Release Date")] public DateTime SystemReleaseDate { get; set; } public IEnumerable AllGames { get; set; } public IEnumerable AllSystems { get; set; } public IEnumerable AllPublishers { get; set; } public IEnumerable GetGameList() { return new[] { new SelectListItem { Value = "0", Text = "--none--" } } .Concat(AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name })); } private static string MakeTable(int cellsPerRow, IEnumerable items, Func cellDataCallback) { var table = new TagBuilder("table"); var count = 0; var row = new TagBuilder("tr"); var cell = new TagBuilder("td"); foreach (var item in items) { if (count % cellsPerRow == 0) { if (count > 0) { table.InnerHtml += row.ToString(TagRenderMode.Normal); } row.InnerHtml = string.Empty; } cell.InnerHtml = cellDataCallback(item); row.InnerHtml += cell.ToString(TagRenderMode.Normal); count++; } if (count > 0) { //last row table.InnerHtml += row.ToString(TagRenderMode.Normal); } return table.ToString(TagRenderMode.Normal); } public string MakePublisherTable(HtmlHelper html, int cellsPerRow = 4) { return MakeTable(cellsPerRow, AllPublishers, publisher => { var id = "publisher_" + publisher.Id; return html.CheckBox("PublisherIds", new { id }) + html.Label(publisher.Name, id); }); } public string MakeSystemTable(HtmlHelper html, int cellsPerRow = 8) { return MakeTable(cellsPerRow, AllSystems, system => { var id = "system_" + system.Id; return html.CheckBox("SystemIds", new { id }) + html.Label(system.Abbreviation, id, new { title = system.Name }); }); } } }