SO-37161202 DropDownList and ViewBag

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
	public class SampleViewModel
	{
		public int CustomerA { get; set; }
		public int CustomerB { get; set; }
		public int CustomerC { 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">
				<h1>Hello Stranger</h1>
			
				
	
				@using (Html.BeginForm())
				{
					<div>Using the same name for both the model and ViewBag property and the DropDownListFor method. The value of CustomerA is "Client 2" but it is not selected</div>
					<div class="form-group">
						@Html.LabelFor(m => m.CustomerA)
						@Html.DropDownListFor(m => m.CustomerA, (IEnumerable<SelectListItem>)ViewBag.CustomerA, "Please select", new { @class="form-control"}) 
						@Html.ValidationMessageFor(m => m.CustomerA)
					</div>
					<div>Using the same name for both the model and ViewBag property and the DropDownList method. There is no client side validation</div>
					<div class="form-group">
						@Html.LabelFor(m => m.CustomerB)
						@Html.DropDownList("CustomerB", (IEnumerable<SelectListItem>)ViewBag.CustomerB, "Please select", new { @class="form-control"}) 
						@Html.ValidationMessageFor(m => m.CustomerB)
					</div>
					<div>Using different name for both the model and ViewBag property and the DropDownListFor method. The correct option is selected and there is client side validation</div>

					<div class="form-group">
						@Html.LabelFor(m => m.CustomerC)
						@Html.DropDownListFor(m => m.CustomerC, (IEnumerable<SelectListItem>)ViewBag.CustomerCList, "Please select", new { @class="form-control"}) 
						@Html.ValidationMessageFor(m => m.CustomerC)
					</div>
				
					<button type="submit" class="btn btn-success submit">Submit</button>
				}

			</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()
		{
			ViewBag.CustomerA = new List<SelectListItem>
			{
				new SelectListItem(){ Value = "1", Text = "Customer 1" },
				new SelectListItem(){ Value = "2", Text = "Customer 2" }
			};
			ViewBag.CustomerB = new List<SelectListItem>
			{
				new SelectListItem(){ Value = "1", Text = "Customer 1" },
				new SelectListItem(){ Value = "2", Text = "Customer 2" }
			};
			ViewBag.CustomerCList = new List<SelectListItem>
			{
				new SelectListItem(){ Value = "1", Text = "Customer 1" },
				new SelectListItem(){ Value = "2", Text = "Customer 2" }
			};
			var model = new SampleViewModel()
			{
				CustomerA = 2,
				CustomerB = 2,
				CustomerC = 2
			};
			return View(model);
		}
		
		[HttpPost]
		public ActionResult Index(SampleViewModel model)
		{
			return View(model);
		}

		
	}
}
;