我是asp.net mvc核心的新手,并希望使用asp.net core mvc中的实体框架在复选框上执行crud操作。我已经完成了文本框的crud操作,但面对多个复选框的问题,所以你能为我提供一步一步实现的链接。
答案 0 :(得分:1)
您只需创建<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);
}
}
}
@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>