简单标记帮助程序的使用-简单net core 程序的架构

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.运行,看效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值