我在我的视图中有这个Dropdownlist,它从我的数据库读取,它当前读取正确的东西,但它只读取每个字符。例如,如果我有" ATL"作为我位置表中的位置,我将得到3个项目的下拉列表" A"," T"," L"而不只是一个项目" ATL"。
我尝试过只使用常规@Html.DropDownList
并让DDL看起来不错,但我一直得到"System.Data.Entity.DynamicProxies.Location.VYFY456886V..."
和#34; ATL"在我的DDL中最终得到2个项目,即使我的表中有1个项目,我也无法找到如何摆脱奇怪的"System.Data"
事物。
所以我认为使用DropDownListFor()
对我的数据库会更好,因为它在我的DDL中没有给我"System"
这个东西。
这是我的控制器:
public ActionResult TakeInventory()
{
CTS_InventoryEntities entities = new CTS_InventoryEntities();
return View(from Asset in entities.Assets select Asset)
}
我有一张名为" Asset"和一个名为" Location"的表。 Asset表具有Location表(LocationKey)中的FK。
这是我的观点:
@model IEnumerable<CTS_Inventory.Models.Asset>
@foreach (var item in Model)
{
<div class="row">
<div class="col-md-6">
<label>Item: </label>
@Html.TextBoxFor(m => item.AssetKey, new { @placeholder = "Enter Asset Tag", @Value = "" })
</div>
<div class="col-md-6">
<label>Inventory Owner: </label>
@Html.TextBoxFor(m => item.InventoryOwner)
</div>
<div class="col-md-6">
<label>Location: </label>
@Html.DropDownListFor(m => item.Location, new SelectList (item.Location.LocationName))
</div>
</div>
}
我只是试图从DDL中选择LocationName而不将其分解为单独的字符。任何帮助将不胜感激。我已经坚持了很长一段时间。谢谢
编辑:
我的AssetViewModel:
public partial class Asset
{
public int AssetKey { get; set; }
public int ProductKey { get; set; }
public int ManufacturerKey { get; set; }
public int ModelKey { get; set; }
public int LocationKey { get; set; }
public int ClientSiteKey { get; set; }
public string SerialNumber { get; set; }
public string ItemName { get; set; }
public string PurchaseDate { get; set; }
public string InventoryOwner { get; set; }
public string InventoriedBy { get; set; }
public System.DateTime InventoryDate { get; set; }
public bool IsDisposed { get; set; }
public virtual ClientSite ClientSite { get; set; }
public virtual Location Location { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual Model Model { get; set; }
public virtual Product Product { get; set; }
}
我的LocationViewModel:
public partial class Location
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Location()
{
this.Assets = new HashSet<Asset>();
}
public int LocationKey { get; set; }
public IEnumerable <SelectListItem> LocationName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Asset> Assets { get; set; }
}
更新:
控制器:
public ActionResult ViewInventory()
{
CTS_InventoryEntities entities = new CTS_InventoryEntities();
ViewBag.LocationKey = new SelectList(entities.Locations, "LocationKey", "LocationName");
ViewBag.ClientSiteKey = new SelectList(entities.ClientSites, "ClientSiteKey", "ClientSiteName");
return View();
LocationViewModel:
public partial class LocationViewModel
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public LocationViewModel()
{
this.Assets = new HashSet<Asset>();
}
public int LocationKey { get; set; }
public string LocationName { get; set; }
public int LocationList { get; set; }
public int ClientSiteKey { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Asset> Assets { get; set; }
}
更新2:
将此添加到我的LocationViewModel:
public IEnumerable<SelectListItem> LocationList { get; set; }
public string LocationSelected { get; set; }
将此添加到我的控制器:
CTS_InventoryEntities entities = new CTS_InventoryEntities();
LocationViewModel model = new LocationViewModel()
{
LocationList = new SelectList(entities.Locations,"LocationKey","LocationName")
};
将此添加到我的视图中:
<label>Location: </label>
@Html.DropDownListFor(m => m.LocationSelected,Model.LocationList)
答案 0 :(得分:0)
请做一件事。
首先从数据库创建SelectLsitItem列表并填写lt。
public List<SelectListItem> GetCity(ref string StateCode, string ZipCode)
{
List<SelectListItem> lstCity = new List<SelectListItem>();
try
{
SqlParameter[] parameters = { new SqlParameter("@StateCode", StateCode), new SqlParameter("@ZipCode", ZipCode) };
DataSet dsCity = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "GetCityByStateCodeZipCode", parameters);
if (dsCity != null && dsCity.Tables.Count > 0 && dsCity.Tables[0].Rows.Count > 0)
{
lstCity = (from drCity in dsCity.Tables[0].AsEnumerable()
select new SelectListItem { Text = drCity["City"].ToString(), Value = drCity["City"].ToString() }).ToList();
if (string.IsNullOrEmpty(StateCode))
{
StateCode = dsCity.Tables[0].Rows[0]["StateCode"].ToString();
}
}
return lstCity;
}
catch (Exception ex)
{
return lstCity;
}
}
将SelectLsitItem存储到viewbag中后。
ViewBag.GLDeductible=GetCity("A","B");
将该视图包分配到下拉列表后。
@Html.DropDownListFor(Model => Model.otherLimitsMappingModel.DamageToPremises, ViewBag.GLDeductible as IEnumerable<SelectListItem>})
这对你有用。
如果你卡在我的代码中,请告诉我,我会帮助你。
感谢。