私は、エンティティ「Person」で構成されるエンティティ「FuelCard」を持っています(新しいカードにリンクされている人を選択する必要があります)。そのため、モデル「Person」の属性「Name」で埋められるドロップダウンリストがあります。私が実行するのは、ドロップダウンリストに名と姓の2つのアイテムを提供することです(したがって、ドロップダウンリストには "FirstName +" "+ LastName"が含まれます)。それをするためのアイデアはありますか?それについてインターネット上で何も見つかりませんでした...
燃料カードの作成メソッド
[HttpPost]
public ActionResult Create(VehicleFuelCard vehiclefuelcard)
{
if (ModelState.IsValid)
{
db.VehicleFuelCards.AddObject(vehiclefuelcard);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id_Person = new SelectList(db.Persons, "Id_Person", "FirstName", vehiclefuelcard.Id_Person);
ViewBag.Id_VehicleFuelCardCompany = new SelectList(db.VehicleFuelCardCompanies, "Id_VehicleFuelCardCompany", "Name", vehiclefuelcard.Id_VehicleFuelCardCompany);
return View(vehiclefuelcard);
}
//
// GET: /VehicleFuelCard/Edit/5
public ActionResult Edit(long id = 0)
{
VehicleFuelCard vehiclefuelcard = db.VehicleFuelCards.Single(v => v.Id_VehicleFuelCard == id);
if (vehiclefuelcard == null)
{
return HttpNotFound();
}
ViewBag.Id_Person = new SelectList(db.Persons, "Id_Person", "FirstName", vehiclefuelcard.Id_Person);
ViewBag.Id_VehicleFuelCardCompany = new SelectList(db.VehicleFuelCardCompanies, "Id_VehicleFuelCardCompany", "Name", vehiclefuelcard.Id_VehicleFuelCardCompany);
return View(vehiclefuelcard);
}
ビューの作成:
@model BuSIMaterial.Models.VehicleFuelCard
@{
ViewBag.Title = "Create";
}
<h2>Create a fuel card</h2><br />
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>VehicleFuelCard</legend>
<div class="editor-label">
Card Number :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Number, new { maxlenght = 50})
@Html.ValidationMessageFor(model => model.Number)
</div>
<div class="editor-label">
PIN code :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.PIN, new { maxlenght = 8})
@Html.ValidationMessageFor(model => model.PIN)
</div>
<div class="editor-label">
Start date :
</div>
<div class="editor-field">
@Html.EditorFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.StartDate)
</div>
<div class="editor-label">
End date :
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EndDate)
@Html.ValidationMessageFor(model => model.EndDate)
</div>
<div class="editor-label">
Company :
</div>
<div class="editor-field">
@Html.DropDownList("Id_VehicleFuelCardCompany", String.Empty)
@Html.ValidationMessageFor(model => model.Id_VehicleFuelCardCompany)
</div>
<div class="editor-label">
Owner :
</div>
<div class="editor-field">
@Html.DropDownList("Id_Person", String.Empty)
@Html.ValidationMessageFor(model => model.Id_Person)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}