[Fork] foreach vs for

CSharp · Mvc

Model

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

namespace HelloWorldMvcApp
{
	
	public class State
	{
		public int Id {get;set;}
		public string Name {get;set;}
		public string Station {get;set;}
	}
	
	public class StateListViewModel
	{
		public List<StateViewModel> States {get;set;}
	}
	
	public class StateViewModel : State
	{		
		public bool Selected {get;set;}
	}
}

View

@model HelloWorldMvcApp.StateListViewModel
@{
	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>@Html.Raw(ViewBag.Message)</h1>
	
				@using (Html.BeginForm())
				{
					<table class="table">
						<thead>
						<tr>
							<th></th>
							<th>
								@Html.DisplayNameFor(model => Model.States[0].Name)
							</th>
							<th>
								@Html.DisplayNameFor(model => Model.States[0].Station)
							</th>
							<th></th>
						</tr>
						</thead>
						<tbody>
						@for (var i = 0; i < Model.States.Count(); i++)
						{
							<tr>
								<td>
									@Html.CheckBoxFor(modelItem => Model.States[i].Selected) @* new bool field *@
								</td>
								<td>
									@Html.DisplayFor(modelItem => Model.States[i].Name)
									@Html.HiddenFor(modelItem => Model.States[i].Name)
								</td>
								<td>
									@Html.DisplayFor(modelItem => Model.States[i].Station)
									@Html.HiddenFor(modelItem => Model.States[i].Station)
								</td>
								<td>
									@Html.ActionLink("Update State Data", "UpdateStation", new { id = Model.States[i].Id }) |
									@Html.ActionLink("Delete", "DeleteState", new { id = Model.States[i].Id })  | Select to Include in Charts
			
								</td>
							</tr>
						}
						</tbody>

        			</table>
        			<input type="submit" value="submit" />
				}
	
				<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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace HelloWorldMvcApp
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			var list = new List<StateViewModel>()
			{
				new StateViewModel(){ Id = 1, Name = "Alabama", Station = "AL"},
				new StateViewModel(){ Id = 2, Name = "Mississippi", Station = "MS"},
				new StateViewModel(){ Id = 3, Name = "Georgia", Station = "GA"},
				new StateViewModel(){ Id = 4, Name = "Florida", Station = "FL"}
			};
			ViewBag.Message = "Select some states";
			
			var model = new StateListViewModel();
			model.States = list;
			
			return View(model);
		}


		[HttpPost]
		public ActionResult Index(StateListViewModel model)
		{
			var message = new StringBuilder();
			message.AppendLine("Selected States<br>");
			model.States.ForEach(a => {
					if (a.Selected == true)
						message.AppendLine(string.Format("{0} {1}<br>", a.Name, a.Station));
				});
			
			ViewBag.Message = message.ToString();
			
			var list = new List<StateViewModel>()
			{
				new StateViewModel(){ Id = 1, Name = "Alabama", Station = "AL"},
				new StateViewModel(){ Id = 2, Name = "Mississippi", Station = "MS"},
				new StateViewModel(){ Id = 3, Name = "Georgia", Station = "GA"},
				new StateViewModel(){ Id = 4, Name = "Florida", Station = "FL"}
			};
			
			model.States = list;
			
			return View(model);
		}
	}
}
;