asp.net - 我如何使用带有剃刀的属性列表来完成 crud
问题描述
我正在使用 .NET 框架和 asp.net 开发一个项目,它是一个可以为保险公司处理某些表格的 Web 应用程序。这个概念是填写表格以完成注册。
为此,我创建了 5 个类,每个类具有以下某些属性:
第一类:
public class BulletinAdhesionRMA
{
[Key]
public int BulletinId { get; set; }
[Required]
[Display(Name = "Type de bulletin")]
public TypeBulletinEnum TypeBulletin { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "N° d'adhésion")]
public string NumeroAdhesion { get; set; }
[Required]
public Employeur Employeur { get; set; }
[Required]
[StringLength(15)]
public string Nom { get; set; }
[Required]
[StringLength(15)]
[Display(Name = "Prénom")]
public string Prenom { get; set; }
[StringLength(15)]
[Display(Name = "Nom de jeune fille (Si affilié de sexe féminin)")]
public string NomJeuneFille { get; set; }
[Required]
public Sexe Genre { get; set; }
[Required]
[Display(Name = "Situation de famille")]
public SituationFamille SituationFamiliale { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Date de naissance")]
public DateTime DateNaissance { get; set; }
[Required]
[Display(Name = "Type de document d'identification (Cin, passeport, etc...)")]
public TypeDocumentIdentification TypeDocument { get; set; }
[Required]
[StringLength(15)]
[Display(Name = "N° document")]
public string NumeroDocument { get; set; }
[Required]
[StringLength(60)]
public string Adresse { get; set; }
[Required]
[StringLength(20)]
public string Ville { get; set; }
[Required]
[StringLength(10)]
public string Pays { get; set; }
[StringLength(15)]
[Phone]
[Display(Name = "Téléphone")]
public string Telephone { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Date d'affiliation")]
public DateTime DateAffiliation { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Date d'entrée en fonction")]
public DateTime DateEntreeFonction { get; set; }
[Display(Name = "Catégorie du personnel")]
[StringLength(50)]
public string CategoriePersonnel { get; set; }
[StringLength(50)]
public string Emploi { get; set; }
[StringLength(50)]
public string Matricule { get; set; }
[Display(Name = "Salaire annuel (ou capital)")]
public double SalaireAnnuel { get; set; }
public List<BeneficiaireConjoint> BeneficiareConjoints { get; set; }
public List<BeneficiaireEnfant> BeneficiareEnfants { get; set; }
public List<BeneficiaireEnCasDeces> BeneficiareEnCasDeces { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Le :")]
public DateTime FaitLe { get; set; }
[Required]
[StringLength(60)]
[Display(Name = "Fait à")]
public string FaitA { get; set; }
}
第 2 类:
public class Employeur
{
[Key,ForeignKey("BulletinAdhesionRMA")]
public int EmployeurId { get; set; }
public BulletinAdhesionRMA BulletinAdhesionRMA { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "N° Client")]
public string NumeroClient { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "N° Filiale")]
public string NumeroFiliale { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Raison Social")]
public string RaisonSocial { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "N° Contrat")]
public string NumeroContrat { get; set; }
[Required]
[StringLength(50)]
public string Adresse { get; set; }
}
第 3 类:
public class BeneficiaireConjoint
{
[Key]
public string Code { get; set; }
public int? BulletinId { get; set; }
[ForeignKey("BulletinId")]
public BulletinAdhesionRMA BulletinAdhesionRMA { get; set; }
[StringLength(15)]
public string Nom { get; set; }
[StringLength(15)]
[Display(Name = "Prénom")]
public string Prenom { get; set; }
public Sexe? Genre { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date de naissance")]
public DateTime? DateNaissance { get; set; }
}
第 4 类:
public class BeneficiaireEnfant
{
[Key]
public string Code { get; set; }
public int? BulletinId { get; set; }
[ForeignKey("BulletinId")]
public BulletinAdhesionRMA BulletinAdhesionRMA { get; set; }
[StringLength(15)]
public string Nom { get; set; }
[StringLength(15)]
[Display(Name = "Prénom")]
public string Prenom { get; set; }
public Sexe? Genre { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date de naissance")]
public DateTime? DateNaissance { get; set; }
}
5级:
public class BeneficiaireEnCasDeces
{
[Key]
public string Code { get; set; }
public int? BulletinId { get; set; }
[ForeignKey("BulletinId")]
public BulletinAdhesionRMA BulletinAdhesionRMA { get; set; }
[StringLength(15)]
public string Nom { get; set; }
[StringLength(15)]
[Display(Name = "Prénom")]
public string Prenom { get; set; }
public Sexe? Genre { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date de naissance")]
public DateTime? DateNaissance { get; set; }
}
之后,我创建了“DbContext”以创建数据库结构:
public class BulletinAdhesionDbContext : DbContext
{
public BulletinAdhesionDbContext() : base("BulletinAdhesionConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BulletinAdhesionDbContext, Configuration>());
}
public DbSet<BulletinAdhesionRMA> BulletinAdhesionsRMA { get; set; }
public DbSet<Employeur> Employeurs { get; set; }
public DbSet<BeneficiaireConjoint> BeneficiaireConjoints { get; set; }
public DbSet<BeneficiaireEnfant> BeneficiaireEnfants { get; set; }
public DbSet<BeneficiaireEnCasDeces> BeneficiaireEnCasDeces { get; set; }
}
此外,我将需要一个“控制器”来制作它们应该是的东西:
public class BulletinAdhesionRMAController : Controller
{
public BulletinAdhesionDbContext db = new BulletinAdhesionDbContext();
// GET: BulletinAdhesionRMA
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "" )] BulletinAdhesionRMA bulletinAdhesionRMA)
{
if(ModelState.IsValid)
{
db.BulletinAdhesionsRMA.Add(bulletinAdhesionRMA);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View();
}
}
最后是负责表单填写的视图:
@model BulletinAdhesion.Models.BulletinAdhesionRMA
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "BulletinAdhesionRMA", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="row" id="rma" >
<div class="col-12">
<div class="row">
<div class="col-sm-4">
<img class="img-responsive" src="~/logos/rma.png" width="50" />
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="m-b-10">
@Html.LabelFor(model => model.TypeBulletin)
@Html.EnumDropDownListFor(model => model.TypeBulletin, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(model => model.TypeBulletin, "", new { @class = "text-danger" })
</div>
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.NumeroAdhesion)
@Html.EditorFor(Model => Model.NumeroAdhesion, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.NumeroAdhesion, "", new { @class = "text-danger" })
</div>
</div>
<div class="card-header custom-card-header">
<h4>Employeur : </h4>
</div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.Employeur.NumeroClient)
@Html.EditorFor(Model => Model.Employeur.NumeroClient, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Employeur.NumeroClient, "", new { @class = "text-danger" })
</div>
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.Employeur.NumeroContrat)
@Html.EditorFor(Model => Model.Employeur.NumeroContrat, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Employeur.NumeroContrat, "", new { @class = "text-danger" })
</div>
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.Employeur.NumeroFiliale)
@Html.EditorFor(Model => Model.Employeur.NumeroFiliale, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Employeur.NumeroFiliale, "", new { @class = "text-danger" })
</div>
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.Employeur.RaisonSocial)
@Html.EditorFor(Model => Model.Employeur.RaisonSocial, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Employeur.RaisonSocial, "", new { @class = "text-danger" })
</div>
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(Model => Model.Employeur.Adresse)
@Html.EditorFor(Model => Model.Employeur.Adresse, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Employeur.Adresse, "", new { @class = "text-danger" })
</div>
</div>
<div class="card-header custom-card-header">
<h4>Adhérent : </h4>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Nom)
@Html.EditorFor(model => model.Nom, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Nom, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Prenom)
@Html.EditorFor(model => model.Prenom, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Prenom, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.NomJeuneFille)
@Html.EditorFor(model => model.NomJeuneFille, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.NomJeuneFille, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Genre)
@Html.EnumDropDownListFor(model => model.Genre, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.DateNaissance)
@Html.EditorFor(model => model.DateNaissance, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.DateNaissance, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.SituationFamiliale)
@Html.EnumDropDownListFor(model => model.SituationFamiliale, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(model => model.SituationFamiliale, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.TypeDocument)
@Html.EnumDropDownListFor(model => model.TypeDocument, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(model => model.TypeDocument, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.NumeroDocument)
@Html.EditorFor(model => model.NumeroDocument, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.NumeroDocument, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Adresse)
@Html.EditorFor(model => model.Adresse, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Adresse, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Ville)
@Html.EditorFor(model => model.Ville, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Ville, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Pays)
@Html.EditorFor(model => model.Pays, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Pays, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Telephone)
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.DateAffiliation)
@Html.EditorFor(model => model.DateAffiliation, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.DateAffiliation, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.DateEntreeFonction)
@Html.EditorFor(model => model.DateEntreeFonction, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.DateEntreeFonction, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.CategoriePersonnel)
@Html.EditorFor(model => model.CategoriePersonnel, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.CategoriePersonnel, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Emploi)
@Html.EditorFor(model => model.Emploi, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Emploi, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.Matricule)
@Html.EditorFor(model => model.Matricule, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.Matricule, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.SalaireAnnuel)
@Html.EditorFor(model => model.SalaireAnnuel, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.SalaireAnnuel, "", new { @class = "text-danger" })
</div>
</div>
<div class="card-header custom-card-header">
<h4>Bénéficiaire (s) Prestations maladie : </h4>
</div>
<div class="row" style="margin-bottom:25px !important;">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Code</th>
<th>Nom (jeune fille)</th>
<th>Prénom</th>
<th>Sexe</th>
<th>Date de naissance</th>
</tr>
</thead>
@*<tbody>
<tr>
<td>CO1</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[0].Nom, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[0].Nom, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[0].Prenom, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[0].Prenom, "", new { @class = "text-danger" })</td>
<td>@Html.EnumDropDownListFor(model => model.BeneficiareConjoints[0].Genre, new { @class = "form-control" })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[0].Genre, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[0].DateNaissance, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[0].DateNaissance, "", new { @class = "text-danger" })</td>
</tr>
<tr>
<td>CO2</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[1].Nom, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[1].Nom, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[1].Prenom, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[1].Prenom, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[1].Genre, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[1].Genre, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareConjoints[1].DateNaissance, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareConjoints[1].DateNaissance, "", new { @class = "text-danger" })</td>
</tr>
</tbody>
<thead>
<tr>
<th>Code</th>
<th>Prénom</th>
<th>Sexe</th>
<th>Date de naissance</th>
</tr>
</thead>
<tbody>
@for (int i = 1; i <= 11; i++)
{
<tr>
<td>EO @i</td>
<td>@Html.EditorFor(model => model.BeneficiareEnfants[i - 1].Prenom, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareEnfants[i - 1].Prenom, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareEnfants[i - 1].Genre, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareEnfants[i - 1].Genre, "", new { @class = "text-danger" })</td>
<td>@Html.EditorFor(model => model.BeneficiareEnfants[i - 1].DateNaissance, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.BeneficiareEnfants[i - 1].DateNaissance, "", new { @class = "text-danger" })</td>
</tr>
}
</tbody>*@
</table>
</div>
</div>
<div class="card-header custom-card-header">
<h4>Bénéficiaire (s) en cas de décès : </h4>
</div>
<div class="row">
@*@for (int i = 0; i < 4; i++)
{
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.BeneficiareEnCas[i].Prenom)
@Html.EditorFor(model => model.BeneficiareEnCas[i].Prenom, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.BeneficiareEnCas[i].Prenom, "", new { @class = "text-danger" })
</div>
}*@
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.FaitA)
@Html.EditorFor(model => model.FaitA, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.FaitA, "", new { @class = "text-danger" })
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(model => model.FaitLe)
@Html.EditorFor(model => model.FaitLe, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.ValidationMessageFor(model => model.FaitLe, "", new { @class = "text-danger" })
</div>
</div>
</div>
<input type="submit" value="Create" class="btn btn-info" />
</div>
}
正如您在“控制器”中看到的,我只保存简单属性的数据,但如果您注意到视图中有一个注释表,该表包含“BeneficiaireConjoint”和“BeneficiaireEnfant”的列表,即两个属性是“BulletinAdhesionRMA”的属性,但它们是列表的类型。
到目前为止一切顺利,但是当我在控制器中尝试此代码时,它不起作用。
public class BulletinAdhesionRMAController : Controller
{
public BulletinAdhesionDbContext db = new BulletinAdhesionDbContext();
// GET: BulletinAdhesionRMA
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "" )] BulletinAdhesionRMA bulletinAdhesionRMA)
{
if(ModelState.IsValid)
{
db.BulletinAdhesionsRMA.Add(bulletinAdhesionRMA);
db.BeneficiaireConjoints.Add(bulletinAdhesionRMA.BeneficiareConjoints);
db.BeneficiaireEnfants.Add(bulletinAdhesionRMA.BeneficiareEnfants);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View();
}
}
我不知道如何从控制器处理该列表,也不知道如何用剃刀处理视图。
我真的很感激一些想法。
先感谢您。
解决方案
推荐阅读
- scala - JOIN后所有相似数据框列的Spark Scala动态比较
- vue.js - 在计算属性 vue 返回的数据中搜索
- python - 如何使用 Selenium 和 Python 根据前面的文本或类名查找文本
- reactjs - 当鼠标悬停在行上时,表格行重新渲染子组件以做出反应
- r - 如何摆脱 Rmd 中 geom_smooth 的评论?
- kdb - KDB+/Q:如何选择字典中的所有项(键+值),其中相应的值符合条件?
- c# - 在 .NET Core 中运行多个 sql 查询以获取矩阵结果
- flutter - Flutter 小部件与 Flutter 驱动程序
- arrays - 使用 VB.net 和 Newtonsoft 对值矩阵进行 JSON 反序列化
- laravel - 通过 Ajax 方法渲染 Laravel 组件