1.新建asp net core 项目
2.在Models下面新建ViewModelBase
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.Models
{
public class ViewModelBase
{
public ViewModelBase(string title = "")
{
Title = title;
ErrorMessage = "";
StatusCode = 0;
}
public string Title { get; set; }
public string ErrorMessage { get; set; }
public int StatusCode { get; set; }
}
}
3 在Models下面新建HomeViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.Models
{
public class HomeViewModel:ViewModelBase
{
public HomeViewModel(string title):base(title)
{
}
}
}
4 在Models下面新建RoomViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.Models
{
public class RoomViewModel:ViewModelBase
{
public RoomViewModel(string title) : base(title)
{
RoomTypes = new List<string>();
}
public IList<string> RoomTypes { get; set; }
public RoomCategories CurrentRoomType { get; set; }
}
public enum RoomCategories
{
[Display(Name = "Not specified")]
None,
Single,
[Display(Name = "Very large room")]
Double
}
}
5 在项目目录下新建App目录,在下面新建服务基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.App
{
public class AppServiceBase
{
}
}
6 在App下面新建HomeService
using MyRazor.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.App
{
public class HomeService : AppServiceBase
{
public HomeViewModel GetHomeViewModel()
{
return new HomeViewModel("MyHomeViewModel");
}
public RoomViewModel GetRoomViewModel()
{
return new RoomViewModel("MyRoomViewModel")
{
RoomTypes = new string[] { "SINGLE", "DOUBLE", "TWIN" },
CurrentRoomType = RoomCategories.Single
};
}
}
}
7.在Startup里面注册HomeService
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton<HomeService>();
}
8,在Controller里面注入服务,并修改Index方法
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MyRazor.App;
using MyRazor.Models;
namespace MyRazor.Controllers
{
public class HomeController : Controller
{
private readonly HomeService homeService;
public HomeController(HomeService service)
{
homeService = service;
}
public IActionResult Index()
{
var model = homeService.GetRoomViewModel();
return View(model);
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Room()
{
return Content("Room");
}
}
}
10修改Index视图
@model MyRazor.Models.RoomViewModel
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<a asp-action="Room" asp-controller="Home" asp-hello="Hello">Room</a>
<div class="form-group">
<label class="col-md-4" asp-for="Title">Title</label>
<div class="col-md-4">
<input class="form-control input-group-lg" asp-for="Title" />
</div>
<div asp-validation-summary="All"></div>
</div>
<div class="form-group">
<select id="room" name="room" class="form-control"
asp-for="@Model.CurrentRoomType"
asp-items="@Html.GetEnumSelectList(typeof(RoomCategories))">
</select>
</div>
11.运行,看效果