Stackoverflow 34511090

CSharp · Mvc

Model

using System;
using System.Collections.Generic;

namespace HelloWorldMvcApp
{
	public class SampleViewModel
	{
		public string Category { get; set; }

		public List<string> SubCategories { get; set; }
	}

	public class GopreadyOnlineTest
	{
	    public string CategoryId { get; set; }
	    public string CategoryName { get; set; }
	    public string SubCategoryId { get; set; }
	    public string SubCategoryName { get; set; }
	    public string Question { get; set; }
	    public string OptionA { get; set; }
	    public string OptionB { get; set; }
	    public string OptionC { get; set; }
	    public string OptionD { get; set; }
	    public string QuestionDescription { get; set; }
	}
}

View

@model HelloWorldMvcApp.SampleViewModel
@{
    Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

        <!-- CSS Includes -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        
        <style type="text/css">

            .field-validation-error {
                color: #ff0000;
            }

        </style>
    </head>
    
    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <button id="get" type="button" class="btn btn-success submit">Get</button>
            </div>
            <div id="examList" class="col-md-6 col-md-offset-3">
                
            </div>
        </div>

        <!-- JS includes -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
        
        <script type="text/javascript">
            $(function(){
                function GetOnlineTestTitle() {    
                    $.ajax({
                        type: "GET",
                        url: '@Url.RouteUrl(new{ action="GetOnlineTestTitle", controller="Home"})',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function (msg) { 
                            $("#examList").html("");
                            var divs = msg.map(function (item) {
                                var $div = $("<div>"); 
                                var $ul = $("<ul>"); 
                                var $h3 = $("<h3>").text(item.Category);
                                
                                item.SubCategories.forEach(function (itemSub) {
                                    $ul.append($("<li>").text(itemSub));
                                });
                                $div.append($h3);
                                $div.append($ul);
								return $div;
                            });
                            $('#examList').append(divs);
                        },
                        error: function (msg) {
                            $("body").html(msg.responseText)
                        }
                    });
                }

                $("#get").click(GetOnlineTestTitle);
            });

        </script>
    </body>
</html>

Controller

using System;
using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;

namespace HelloWorldMvcApp
{
    public class HomeController : Controller
    {
        List<GopreadyOnlineTest> list;

        public HomeController()
        {
            this.list = new List<GopreadyOnlineTest> 
            {
                new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Basic General Knowledge" },
                new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "World Geography" },
                new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Inventions" },
                new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Honours and Awards" },
                new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Time & Speed" },
                new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Algebra" },
                new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Accounts" }
            };
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View(new SampleViewModel());
        }

        [HttpGet]
        public JsonResult GetOnlineTestTitle()
        {               
            var result = list.GroupBy(x => x.CategoryName).Select(c => new SampleViewModel { Category = c.Key, SubCategories = c.Select(sc => sc.SubCategoryName).Distinct().ToList() } );
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private static Random _rnd = new Random();
        
        private static List<string> _db = new List<string> { "Yes", "No", "Definitely, yes", "I don't know", "Looks like, yes"} ;
    }
}

;