[Fork] SO 26479693 (2nd)

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
	public class SampleViewModel
	{
		[Required]
		[MinLength(10)]
		[MaxLength(100)]
		[Display(Name = "Ask Magic 8 Ball any question:")]
		public string Question { get; set; }

		//See here for list of answers
		public string Answer { 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">
		
	</head>
	
	<body>
		<div class="container">
			
			@Html.DropDownList("Country","Select country")

		</div>
	</body>
</html>

Controller

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

namespace HelloWorldMvcApp
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
	       List<SelectListItem> ListItems = new List<SelectListItem>();
			
                ListItems.Add(new SelectListItem
                {
                    Text = "USA",
                    Value = "1"
                });
			
			 ListItems.Add(new SelectListItem
                {
                    Text = "India",
                    Value = "2"
                });
			
                ViewBag.Country = ListItems;

			return View(new SampleViewModel());
		}
	}
}
;