CSharp · Mvc
using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Web.Mvc;
namespace HelloWorldMvcApp
{
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public Site SiteID { get; set; }
}
public class Site
{
public Site(int id, string siteName)
{
this.ID = id;
this.SiteName = siteName;
}
public int ID { get; set; }
public string SiteName { get; set; }
}
public class MyViewModel
{
public MyViewModel()
{
this.Sites = new List<Site>();
}
public int ID { get; set;}
public Student Students { get; set; }
public DateTime Date { get { return DateTime.Now; } }
public bool Criteria { get; set; }
public List<Site> Sites { get; set; }
}
}
@model HelloWorldMvcApp.MyViewModel
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h3>DropDownListFor</h3>
@Html.DropDownListFor(m => m.Sites,
new SelectList(Model.Sites, "Id", "SiteName"))
<h3>EditorFor</h3>
@Html.EditorFor(m => m)
</body>
</html>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
public ActionResult Index()
{
var vm = SeedFromDatabase();
return View(vm);
}
private MyViewModel SeedFromDatabase()
{
var vm = new MyViewModel();
vm.Sites.Add(new Site(0, "one"));
vm.Sites.Add(new Site(1, "two"));
vm.Sites.Add(new Site(2, "three"));
return vm;
}
}
}