必须在多个复选框上进行 CRUD 操作

CRUD operation necessarily on multiple check boxes

我是 asp.net mvc core 的新手,想使用 asp.net core mvc 中的 entity framework 对复选框进行 crud 操作。我已经对文本框进行了 crud 操作,但遇到了多个复选框的问题,所以你能给我提供 link 吗,它将教你逐步实施。

您只需创建 <input type="checkbox" /> 并使用标签助手 asp-for 将其绑定到您的 View Model.

中的属性

查看模型

using System.ComponentModel.DataAnnotations;

namespace DL.SO.Project.Reporting.Accounts
{
    public class LoginViewModel
    {
        [Required]
        public string Username { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }

        public string ReturnUrl { get; set; }
    }
}

控制器

using Microsoft.AspNetCore.Authentication;
using DL.SO.Project.Reporting.Accounts;

namespace DL.SO.Project.Web.UI.Controllers
{
    public class AccountController : Controller
    {
        [AllowAnonymous]
        public IActionResult Login(string returnUrl)
        {
            var vm = new LoginViewModel
            {
                ReturnUrl = returnUrl,
                // Default the checkbox to checked
                RememberMe = true
            };

            return View(vm);
        }
    }
}

Razor 视图(使用 Bootstrap 4.0 CSS 框架)

@model DL.SO.Project.Reporting.Accounts.LoginViewModel
@{
    ViewData["Title"] = "Login";
}

<form asp-area="" asp-controller="account" asp-action="login">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input type="hidden" asp-for="ReturnUrl" />

    <div class="form-group">
        <label asp-for="Username" class="required"></label>
        <input type="text" class="form-control" asp-for="Username" autofocus="autofocus" />
        <span class="form-text" asp-validation-for="Username"></span>
    </div>
    <div class="form-group">
        <label asp-for="Password" class="required"></label>
        <input type="password" class="form-control" asp-for="Password" autocomplete="off" />
        <span class="form-text" asp-validation-for="Password"></span>
    </div>
    <div class="form-group">
        <!--
            This is how you bind the property "RememberMe" from the 
            view model to the input
        -->
        <div class="custom-control custom-checkbox">               
            <input type="checkbox" class="custom-control-input" asp-for="RememberMe" />
            <label asp-for="RememberMe" class="custom-control-label"></label>
        </div>
    </div>
    <button type="submit" class="btn btn-primary btn-block">Login</button>
</form>