View视图中:
@using MedCrab.Core.Model.APP
@model User
学校: @Html.DropDownListFor(m => m.fSchool, ViewBag.School as SelectList)
Controller中:
[HttpGet]
//对应一个页面,服务器向客户端发送一个页面
//URl带参数:http://localhost:8001/my/#/Test/Test?userId=7dd555b1-95ac-4872-9a3c-28954da26cfe
public ActionResult Test(string userId)
{
string[] schoolSelect = new string[] { "四川理工", "西南交通", "成都理工", "重庆邮电" };//可以从数据库中进行存取
if (string.IsNullOrEmpty(userId))
//为空,直接返回视图
{
//设置数据,否则报错不存在具有键“fSchool”的“IEnumerable<SelectListItem>”类型的 ViewData 项。
ViewBag.School = new SelectList(schoolSelect);
return View();
}
//不为空,将已有信息呈现在视图中
else
{
userService = new UserService();
User user = (User)userService.GetModelByID(userId);
//使用数据库的默认值
ViewBag.School = new SelectList(schoolSelect, user.fSchool);
return View(user);
}
}