I am building an ASP.NET MVC 5 application and I am pretty new to this technology. I would be very thankful for every piece of advice, because I got stuck.
I am building a Memory Game. I have already built the JavaScript code of the game and the game works as a standalone code with statically defined array of cards/images for the game. But now I want to put it into the structure of the MVC application, with the cards retrieved from the database.
Model:
using System;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
namespace SmartBunnyApp.Models
{
public class Word
{
public int WordId { get; set; }
public string EnglishWord { get; set; }
public string PolishTranslation { get; set; }
public string Category { get; set; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SmartBunnyApp.Models;
namespace SmartBunnyApp.Models
{
public class WordsListViewModel
{
public IEnumerable<Word> Words { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
public byte[] ImageData { get; set; }
}
}
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmartBunnyApp.Abstract;
using SmartBunnyApp.Models;
namespace SmartBunnyApp.Controllers
{
public class WordController : Controller
{
private IWordRepository repository;
public int PageSize = 4;
public WordController(IWordRepository wordRepository)
{
this.repository = wordRepository;
}
public ViewResult List(string category, int page = 1)
{
WordsListViewModel viewModel = new WordsListViewModel
{
Words = repository.Words
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.WordId)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ?
repository.Words.Count() :
repository.Words.Where(e => e.Category == category).Count()
},
CurrentCategory = category
};
return View(viewModel);
}
public IEnumerable<Word> GetAllWords()
{
return repository.Words;
}
public FileContentResult GetImage(int wordId)
{
Word word = repository.Words.FirstOrDefault(p => p.WordId == wordId);
if (word != null)
{
return File(word.ImageData, word.ImageMimeType);
} else
{
return null;
}
}
}
}
And now I need to have the JavaScript (or as I already got to know) jQuery code, that will help me retrieve the words and their images from the database for the chosen category and put them into a simple multidimensional table, like this one:
var memory_matching_array = [['dog.jpg','DOG],['penguin.jpg','PENGUIN']];
I tried to implement it the way that someone suggested in the comment below, but I keep getting an error. Especially the part where I have to give the URL in the jQuery function is hard for me (I have no idea how this URL should look like exactly) and then the part when I have to put the data into a table to make it look like the one that I pasted above.
Thank you guys very much for help!