2013-03-05 83 views
0

我有一个实体“燃料卡”,由一个entoty“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") 
} 
+1

是否在添加到SelectList之前将2个字段合并为一个字段? - [看到这里](http://stackoverflow.com/questions/12727285/mvc-selectlist-combining-multiple-columns-in-text-field/12727352#12727352) – StuartLC 2013-03-05 15:25:51

+0

是的,为了有(假设名字= “杰克”和姓氏=“麻雀”)在我的dropdownlist:杰克斯派洛 – Traffy 2013-03-05 15:27:55

+1

检查[这里](http://stackoverflow.com/questions/14687082/combine-2-fields-in-selectlist/14687162#14687162) – 2013-03-05 15:32:28

回答

4

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

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

然后绑定你的下拉菜单。

+0

谢谢你的帮助 ! – Traffy 2013-03-05 15:46:58

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); 
相关问题