0

我有一个实体“Fuel Card”,它由一个实体“Person”组成(您必须选择一个与新卡关联的人)。所以我有一个下拉列表,由我的模型“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")
}
4

2 回答 2

4

在您的模型上创建一个名为 FullName 的属性,类似于

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

然后将你的下拉绑定到那个。

于 2013-03-05T15:28:24.910 回答
1

尝试这个:

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);
于 2013-03-05T15:28:05.253 回答