我有两个实体 Gabarit 和 Local_Gabarit ,我有一个外键,它与那些名为 CodeBarre 的实体相关,后者是 Gabarit 即可。因此,在我的Gabarit编辑页面上,我不想编辑 Gabarit 属性,而是编辑 Local_Gabarit 中存在的相关属性,例如Armoire和Emplacement。 拜托,我需要你的帮助,谢谢:)
Gabarit模型:
public Gabarit()
{
this.Demande = new HashSet<Demande>();
this.Local_Gabarit = new HashSet<Local_Gabarit>();
this.Mvt_Gabarit = new HashSet<Mvt_Gabarit>();
}
public int CodeBarre { get; set; }
public string Designation { get; set; }
public string Photo { get; set; }
public string Exemplaire { get; set; }
public Nullable<int> Produit { get; set; }
public Nullable<int> Poste { get; set; }
public virtual ICollection<Demande> Demande { get; set; }
public virtual ICollection<Local_Gabarit> Local_Gabarit { get; set; }
public virtual ICollection<Mvt_Gabarit> Mvt_Gabarit { get; set; }
Local_Gabarit模型:
public partial class Local_Gabarit
{
public int id_local { get; set; }
public Nullable<int> Emplacement { get; set; }
public string Armoire { get; set; }
public int CodeBarre { get; set; }
public int id_demande { get; set; }
public string InOut { get; set; }
public virtual Demande Demande { get; set; }
public virtual Gabarit Gabarit { get; set; }
}
GabaritViewModel:
public class GabaritViewModel
{
public int CodeBarre { get; set; }
public string Designation { get; set; }
public string Photo { get; set; }
public Nullable<int> Produit { get; set; }
public string Exemplaire { get; set; }
public Nullable<int> Poste { get; set; }
public int id_local { get; set; }
public Nullable<int> Emplacement { get; set; }
public string Armoire { get; set; }
}
GabaritsController:
// GET: Gabarits/Edit/5
public ActionResult Edit(int? id)
{
var localrepository = new LocalGabaritRepository(db);
var dbgabarit = gabaritrepository.GetById(id);
var req = localrepository.Get(p => p.CodeBarre == dbgabarit.CodeBarre).SingleOrDefault();
var armoire = req.Armoire;
var emplacement = req.Emplacement;
if (dbgabarit != null)
{
var gabarit = new GabaritViewModel()
{
CodeBarre = dbgabarit.CodeBarre,
Designation = dbgabarit.Designation,
Photo = dbgabarit.Photo,
Exemplaire = dbgabarit.Exemplaire,
Poste=dbgabarit.Poste,
Produit=dbgabarit.Produit,
Emplacement=emplacement,
Armoire=armoire,
};
return View(gabarit);
}
else
return HttpNotFound();
}
// POST: Gabarits/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit( GabaritViewModel gabarit)
{
var localrepository = new LocalGabaritRepository(db);
if (ModelState.IsValid)
{
var req= localrepository.Get(p => p.CodeBarre == gabarit.CodeBarre).SingleOrDefault();
var armoire = req.Armoire;
var emplacement = req.Emplacement;
var idl = req.id_local;
var idd = req.id_demande;
var InOut = req.InOut;
var CodeBarre = req.CodeBarre;
var dblocal = new Local_Gabarit()
{
Emplacement = emplacement,
Armoire = armoire,
id_local=idl,
id_demande=idd,
InOut=InOut,
CodeBarre=CodeBarre
};
localrepository.Update(dblocal);
}
return View(gabarit);
}
Edit.cshtml(Gabarit):
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Gabarit</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CodeBarre)
<div class="form-group">
@Html.LabelFor(model => model.CodeBarre, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CodeBarre, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.CodeBarre, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" >
@Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Photo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<img src="@Url.Content(Model.Photo)" width="200" height="200" />
@Html.ValidationMessageFor(model => model.Photo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Produit, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Produit, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Produit, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Armoire, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Armoire, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Armoire, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Emplacement, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Emplacement, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Emplacement, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
我不知道我是否以正确的方式行事,但这是我试图做的事情。 谢谢。