Channels ▼
RSS

Open Source

MongoDB with C#: Deep Dive


Add a new class, PlayerController, within the Controllers folder:

namespace RetrogamesWeb.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;

    using Data.Entities;
    using Data.Services;
    using Models;
    using MongoDB.Bson;

    public class PlayerController : Controller
    {
        //
        // GET: /Player/AddScore

        public ActionResult AddScore(string playerId, string gameId, string gameName)
        {
            var playerService = new PlayerService();
            var score = new Score
            {
                GameId = new ObjectId(gameId),
                GameName = gameName,
                ScoreDateTime = DateTime.Now,
                //// Generate a random score
                ScoreValue = new Random().Next(0, Int32.MaxValue)
            };

            playerService.AddScore(playerId, score);

            return RedirectToAction("Details", new { id = playerId});
        }

        //
        // GET: /Player/Create

        public ActionResult Create()
        {
            return View(new Player());
        }

        //
        // POST: /Player/Create

        [HttpPost]
        public ActionResult Create(Player player)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var playerService = new PlayerService();
                    playerService.Create(player);
                    return RedirectToAction("Index");
                }

                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Player/Delete/514b46581cbfe31ad86ec630

        public ActionResult Delete(string id)
        {
            var playerService = new PlayerService();
            var player = playerService.GetById(id);
            return View(player);
        }

        //
        // POST: /Player/Delete/514b46581cbfe31ad86ec630

        [HttpPost]
        public ActionResult Delete(Player player)
        {
            try
            {
                var playerService = new PlayerService();
                playerService.Delete(player.Id.ToString());

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Player/Details/514b46581cbfe31ad86ec630

        public ActionResult Details(string id)
        {
            var playerService = new PlayerService();
            var player = playerService.GetById(id);

            return View(player);
        }

        //
        // GET: /Player/Edit/514b46581cbfe31ad86ec630

        public ActionResult Edit(string id)
        {
            var playerService = new PlayerService();
            var player = playerService.GetById(id);

            return View(player);
        }

        //
        // POST: /Player/Edit/514b46581cbfe31ad86ec630

        [HttpPost]
        public ActionResult Edit(Player player)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var playerService = new PlayerService();
                    playerService.Update(player);
                    return RedirectToAction("Index");
                }

                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Player/

        public ActionResult Index()
        {
            var playerService = new PlayerService();
            var playersDetails = playerService.GetPlayersDetails(100, 0);

            return View(playersDetails);
        }

        //
        // GET: /Player/PlayGames/514b46581cbfe31ad86ec630

        public ActionResult PlayGames(string id)
        {
            var playerService = new PlayerService();
            var player = playerService.GetById(id);
            var gameService = new GameService();
            var availableGames = gameService.GetGamesDetails(100, 0);

            var playerGames = new PlayerGames()
            {
                Player = player,
                AvailableGames = new List<Game>(availableGames)
            };

            return View(playerGames);
        }

    }
}

As you might guess, there is a problem with the Player entity because the GetById method retrieves a Player document with all the embedded score documents. Thus, the PlayerController.PlayGames method that retrieves a player with that method and sets the results to the Player property of the PlayerGames instance is retrieving more information than required. However, I've provided examples of how to retrieve a specific set of fields that you can use to modify this sample application. I also want to show the typical problems that arise while developing the application and working with models that have embedded documents.

Take into account that the code doesn't have the necessary validations. That's something you should already know how to improve with your ASP.NET MVC skills. Because the application allows you to easily generate and retrieve documents in a MongoDB database, it provides you with a baseline for your future work with ASP.NET MVC and MongoDB.

Create a new Game subfolder within the Views folder. Then, add a new view, Create.cshtml, within the new Views\Game subfolder:

@model RetrogamesWeb.Data.Entities.Game

@{
    ViewBag.Title = "Create a new game";
}

<h2>Create a new game</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Game</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>
        
        @{
            var categories = new List<SelectListItem> { 
                new SelectListItem { Text = "remake", Value = "remake" },
                new SelectListItem { Text = "space", Value = "space" },
                new SelectListItem { Text = "shooter", Value = "shooter" }
            };
        }

        <div class="editor-label">
            @Html.LabelFor(model => model.Categories)
        </div>
        <div class="editor-field">
            @Html.ListBoxFor(model => model.Categories, categories)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Add a new view, Index.cshtml, within the Views\Game subfolder:

@model IEnumerable<RetrogamesWeb.Data.Entities.Game>

@{
    ViewBag.Title = "Games";
}

<h2>Games</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th></th>
    </tr>

@foreach (var game in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => game.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => game.ReleaseDate)
        </td>
    </tr>
}

</table>

Add a new view, Index.cshtml, within the Views\Game subfolder:

@model IEnumerable<RetrogamesWeb.Data.Entities.Game>

@{
    ViewBag.Title = "Games";
}

<h2>Games</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th></th>
    </tr>

@foreach (var game in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => game.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => game.ReleaseDate)
        </td>
    </tr>
}

</table>

Now, create a new Player subfolder within the Views folder. Then, add a new view, Create.cshtml, within the new Views\Player subfolder:

@using RetrogamesWeb.Data.Entities
@model RetrogamesWeb.Data.Entities.Player

@{
    ViewBag.Title = "Create a new player";
}

<h2>Create a new player</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Player</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.Gender)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Gender))))
            @Html.ValidationMessageFor(model => model.Gender)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video