我正在使用html dropdownlist帮助从数据库中检索信息。该表包含重复信息,因此我需要使用不同的功能,同时为每个选项元素提供一个唯一值,该值应该是选项元素本身的值。
这是我迄今为止开发的代码。它是截然不同的,但选项元素没有id。
<div class="row">
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding", new SelectList(Model.Select(x => x.type).Distinct()))
</div>
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding", new SelectList(Model.Select(x => x.stories).Distinct()))
</div>
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding", new SelectList(Model.Select(x => x.size)))
</div>
<div class="col-md-offset-2 col-md-1">
<button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-info-sign"></span>
</button>
</div>
更新
以下是该模型的代码:
namespace BIRDSResidential.Models
{
using System;
using System.Collections.Generic;
public partial class ResidentialBuilding
{
public int Id { get; set; }
public string type { get; set; }
public short stories { get; set; }
public int size { get; set; }
public string age { get; set; }
public string orientation { get; set; }
public string shape { get; set; }
public int floorht { get; set; }
public string foundation { get; set; }
public int windowpercent { get; set; }
public string heating { get; set; }
public string cooling { get; set; }
}
}
这是控制器的代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BIRDSResidential.Models;
namespace BIRDSResidential.Controllers
{
public class ResidentialBuildingController : Controller
{
private BIRDSReslocalEntities db = new BIRDSReslocalEntities();
// GET: ResidentialBuilding
public ActionResult Index()
{
//ViewBag.ResidentialBuildings = new SelectList(db.ResidentialBuildings, "ID", "type");
return View(db.ResidentialBuildings.ToList());
}
答案 0 :(得分:0)
首先,在视图层中包含数据访问代码是不好的做法。将其移动到适当的业务逻辑层。
但是,对于您的问题,问题是您没有设置dataValueField和dataTextField。您需要使用不同的重载SelectList构造函数。参考here。从您的示例代码中猜测它应该是这样的:
<强>更新强>
这里有一些问题。关于获取不同的记录,您需要实现IComparable。实现此目的的原因是您要选择一组带有“Id”和“Description”的对象。在仅具有一个属性的一组对象上使用distinct将不允许您正确填充下拉框。
在更新的答案中,我没有这样做,只更新了代码以填充选择框。
更新(控制器):
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BIRDSResidential.Models;
namespace BIRDSResidential.Controllers
{
public class ResidentialBuildingController : Controller
{
private BIRDSReslocalEntities db = new BIRDSReslocalEntities();
// GET: ResidentialBuilding
public ActionResult Index()
{
ViewBag.ResidentialBuildings = new SelectList(db.ResidentialBuildings, "ID", "type").Distinct();
return View(db.ResidentialBuildings.ToList());
}
}
更新(查看):
<div class="row">
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding", (ViewBag.ResidentialBuildings as SelectList))
</div>
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding",(ViewBag.ResidentialBuildings as SelectList))
</div>
<div class="col-md-3">
@Html.DropDownList("ResidentialBuilding", (ViewBag.ResidentialBuildings as SelectList))
</div>
<div class="col-md-offset-2 col-md-1">
<button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-info-sign"></span>
</button>
</div>