grouping

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;

namespace HelloWorldMvcApp
{
	public class QuestionViewModel
	{
		public QuestionViewModel()
		{
			Scores= new List<Score>(){
				new Score() {ScoreId = 1, ScoreNum = 99},
				new Score() {ScoreId = 2, ScoreNum = 98},
				new Score() {ScoreId = 3, ScoreNum = 97},
				new Score() {ScoreId = 4, ScoreNum = 96},
				new Score() {ScoreId = 5, ScoreNum = 95}
			};
		}
		public int StnAssureQuestionId {get;set;}

		public string AreaName {get; set; }
		
		public string Question { get; set; }
		
		public int Score {get;set;}
		
		public string Comments {get;set;}
		
		public List<Score> Scores {get;set;}

	}
	
	public class QuestionListViewModel
	{
		public List<QuestionViewModel> QuestionList {get;set;}
	}
	
	public class Score 
	{
		public int ScoreId {get;set;}
		public int ScoreNum {get;set;}
	}
}

View

@model HelloWorldMvcApp.QuestionListViewModel
@{
	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">
				<h1>Hello Stranger</h1>
	
				@using (Html.BeginForm())
				{
					foreach (var group in Model.QuestionList.GroupBy(x => x.AreaName))
					{
						var questionList = group.ToList();
						<h4>@group.Key</h4>
						for (int i = 0; i < questionList.Count(); i++)
						{
							<div class="form-group">
								<div class="row">
									<div class="col-md-4">
										@Html.DisplayFor(x => questionList[i].Question)
									</div>
									<div class="col-md-2">
										@Html.HiddenFor(x => questionList[i].StnAssureQuestionId)
										@Html.DropDownListFor(model => questionList[i].Score, new SelectList(questionList[i].Scores, "ScoreId", "ScoreNum", questionList[i].Score), "Please Select", new { @class = "form-control", style="padding:0;"})
										@Html.ValidationMessageFor(model => questionList[i].Score, "", new { @class = "text-danger" })
									</div>
									<div class="col-md-4">
										@Html.EditorFor(x => questionList[i].Comments, new { htmlAttributes = new { @class = "form-control" } })
										@Html.ValidationMessageFor(model => questionList[i].Comments, "", new { @class = "text-danger" })
									</div>
								</div>
							</div>
						}
					}
				
					<button type="button" class="btn btn-success submit">Ask</button>
				}
	
				<br/><br/>
				<div class="alert alert-warning fade">
					<img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/>
					<strong><span class="alert-content"></span></strong>
				</div>
			</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">
		</script>
	</body>
</html>

Controller

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

namespace HelloWorldMvcApp
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			var model = new QuestionListViewModel(){
				QuestionList = new List<QuestionViewModel>(){
				new QuestionViewModel() {StnAssureQuestionId = 1, AreaName = "Area1", Question = "Area1 Question1", Score = 1, Comments = ""},
				new QuestionViewModel() {StnAssureQuestionId = 1, AreaName = "Area1", Question = "Area1 Question2", Score = 2, Comments = ""},
				new QuestionViewModel() {StnAssureQuestionId = 1, AreaName = "Area2", Question = "Area2 Question1", Score = 3, Comments = ""},
				new QuestionViewModel() {StnAssureQuestionId = 1, AreaName = "Area2", Question = "Area2 Question2", Score = 4, Comments = ""},
				new QuestionViewModel() {StnAssureQuestionId = 1, AreaName = "Area3", Question = "Area3 Question1", Score = 5, Comments = ""}
				}
			};
			return View(model);
		}
		
		[HttpPost]
		public ActionResult Index(QuestionListViewModel model)
		{
			return View(model);
		}


		[HttpPost]
		public JsonResult GetAnswer(string question)
		{				
			int index = _rnd.Next(_db.Count);
			var answer = _db[index];
			return Json(answer);
		}

		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"} ;
	}
}
;