CSharp · Mvc
using System;
using System.ComponentModel.DataAnnotations;
namespace HelloWorldMvcApp
{
public class Cidades
{
public int id { get; set; }
public string nome { get; set; }
}
}
@model HelloWorldMvcApp.Cidades
@{
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">
@Html.DropDownList("Id", (SelectList)ViewBag.ViewCidade, "- Selecione -", new { @class = "form-control" })
</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>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
public ActionResult Index()
{
var cidade = new Cidades()
{
id = 1,
nome = "São Paulo"
};
var cidades = new List<Cidades>();
cidades.Add(cidade);
ViewBag.ViewCidade = new SelectList(cidades, "id", "nome");
return View(new Cidades());
}
}
}