我想知道我是否以正确的方式做事。我有两个模型:
人员 :
public class RH_Personnel
{
public int RH_PersonnelID { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
}
证明:
public class RH_Attestation
{
public int RH_AttestationID { get; set; }
public virtual RH_Personnel Employe { get; set; }
public string TypeAttestation { get; set; }
}
我使用迁移来生成我的表。我知道我做错了什么,因为当我向数据库添加一个新的时,即使它已经存在,它也会Attestation
创建一个新的。RH_Personnel
我的控制器:
public ActionResult Create()
{
ViewBag.TypeAttestation = new SelectList(db.RH_TypeAttestation.ToList(),"Type","Type");
RH_Attestation Attestation = new RH_Attestation();
Attestation.Employe = (RH_Personnel)HttpContext.Session["Employe"];
return View();
}
//
// POST: /Attestation/Create
[HttpPost]
public ActionResult Create(RH_Attestation rh_attestation)
{
if (ModelState.IsValid)
{
//rh_attestation.Employe = (RH_Personnel)HttpContext.Session["Employe"];
//rh_attestation.DateDemande = DateTime.Now;
//rh_attestation.DateValidation = rh_attestation.DateDemande;
//rh_attestation.Etat = ATTESTATION_ETAT_ENCOURS;
db.RH_Attestation.Add(rh_attestation);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(rh_attestation);
}
我的观点 :
@model Intra.Models.RH_Attestation
@using (Html.BeginForm("Create", "Attestation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form_settings">
@Html.HiddenFor(model => model.Employe.Username)
@Html.HiddenFor(Model => Model.Employe.RH_PersonnelID)
<p>
<span>
@Html.Label("Nom") :
</span>
@Html.EditorFor(model => model.Employe.Nom)
</p>
<p>
<span>
@Html.Label("Prénom") :
</span>
@Html.EditorFor(model => model.Employe.Prenom)
</p>
@Html.DropDownList("TypeAttestation", "Selectionner un type")
<p style="padding-top: 15px;">
<span> </span>
<input type="submit" value="Envoyer" class="submit" />
</p>
</div>
}