- public class HomeController : Controller
- {
- public IActionResult Index()
- {
- return View(); //默认找 Views/Home/Index.cshtml ,呈现给用户
- }
- }
- public class HomeController : Controller
- {
- public IActionResult Index()
- {
- return View("~/Views/Home/abc.cshtml");
- }
- }
视图有它自己的语法,这个语法就是 Razor语法
- @{
- string str = "字符串";
- }
-
abc
@str
- public class UserController : Controller
- {
-
- public IActionResult DoRegister(UserInfo userInfo)
- {
- if (ModelState.IsValid)
- {
- return View("~/Views/User/Detail.cshtml",userInfo);
- }
- }
- }
- @model FirstCoreMvc.Models.UserInfo
-
@Model.Name
- public class HomeController : Controller
- {
- public IActionResult Index()
- {
- var currentUser = new UserInfo() { Name="xiaoxiao",Password="1234"};
- ViewData["currentuser"] = currentUser;
- return View(); //默认找 Views/Home/Index.cshtml ,呈现给用户
- }
- }
- @{
- var currentUser = ViewData["currentuser"] as UserInfo;
- }
@currentUser.Name
也可以在视图( Views/Home/Index.cshtml )中定义 @{ ViewData["Title"] = "Home Page"; } 在_Layout.cshtml 布局页面中使用。
控制器 如下:
ViewBag.currentTime = DateTime.Now.ToShortTimeString();
ViewBag.xiaowang= new UserInfo() { Name = "小王", Password = "1234" };
视图 如下: