I have an entity "Fuel Card" which is composed by an entoty "Person" (you have to choose a person linked to a new card). So I have a dropdownlist which is filled by the attribute "Name" of my model "Person". What I would perform is to provide to my dropdownlist 2 items which are the first name and the last name (so my dropdownlist will contain "FirstName + " " + LastName"). Any idea to do that? Nothing found on the internet about that...
The Create Methods for my Fuel Card
[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);
}
The View Create :
@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")
}