models、controllers、view、ViewModel
1.models
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace school.Models { public class student { public string FristName { get; set; } public string LastName { get; set; } public string Height { get; set; } } }
2.ViewModel
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace school.ViewModels { public class studentViewModel { public string name { get; set; } public string Height { get; set; } } }
3.controllers
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using school.Models; using school.ViewModels; namespace school.Controllers { public class studentController : Controller { // // GET: /student/ public ActionResult Index() { student stu = new student(); stu.FristName = "asa"; stu.LastName = "jqljw"; stu.Height = "656"; studentViewModel stuVM = new studentViewModel(); stuVM.name = stu.FristName + stu.LastName; stuVM.Height = stu.Height; return View("student", stuVM); } } }
4.view
@model school.ViewModels.studentViewModel <h2>student</h2> <p>@Model.name</p> <p>@Model.Height</p>