0

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")
}
Traffy
  • 2,803
  • 15
  • 52
  • 78
  • 1
    Do you mean merging 2 fields into one before adding to a SelectList ? - [see here](http://stackoverflow.com/questions/12727285/mvc-selectlist-combining-multiple-columns-in-text-field/12727352#12727352) – StuartLC Mar 05 '13 at 15:25
  • Yes, in order to have (assuming first name = "jack" and last name = "sparrow") in my dropdownlist : Jack Sparrow – Traffy Mar 05 '13 at 15:27
  • 1
    Check [HERE](http://stackoverflow.com/questions/14687082/combine-2-fields-in-selectlist/14687162#14687162) – AliRıza Adıyahşi Mar 05 '13 at 15:32

2 Answers2

4

Create a property on your model called FullName that is something like

public string FullName
{
    get { return FirstName + " " + LastName; }
}

Then bind your drop down to that.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
1

Try this:

var people = from p in db.Persons.ToList()
             select new
             {
                Id = Id_Person,
                Name = p.FirstName + " " + p.LastName,
             };

ViewBag.Id_Person = new SelectList(people, "Id", "Name", vehiclefuelcard.Id_Person);
Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57