使用IList的下拉列表需要Guid
本文关键字:Guid 下拉列表 IList 使用 | 更新日期: 2023-09-27 17:59:10
我在访问使用Guid参数生成.ToList()的服务时遇到一些问题
public IList<Info> GetBusinessLocations(Guid businessId)
{
using (var db = new DealerContext())
{
return GetDealerDetails(
db.Details
.Join(db.BusinessLocations,
x => x.BusinessId,
y => y.BusinessId, (x, y) => x)
.Where(d => d.BusinessId == businessId), db)
.ToList();
}
}
我尝试过用guid作为参数编写ActionResult,但我仍然收到一个错误,即"parameters dictionary contains a null entry for parameters'businessId'"
public ActionResult Create(Guid businessId)
{
ViewBag.Locations = new SelectList(ServiceBus.GetBusinessLocations(businessId), "BusinessId", "LocationName");
return View(IdGenerator());
}
private static Data.Models.Business IdGenerator()
{
var model = new Models.Business
{
BusinessId = Guid.NewGuid()
};
return (model);
}
在我看来,我只是打电话给的下线
@Html.DropDownListFor(m =>m.BusinessId, (IEnumerable<SelectListItem>)ViewBag.Locations,"Select your Businesses Location")
如果有人能提供一些见解,我将不胜感激。我很难在SO上找到类似的场景。
我最终更改了很多代码,所以我用我的修复程序来结束这个问题。希望这个方法能帮助其他人。
服务查询
public IList<DealerInfo> GetBusinessLocations(Guid? locationId)
{
using (var db = new DealerContext())
{
var query = db.DealerDetails
.Join(db.DealerValidations,
d=>new {bizId=d.BusinessId,lId=d.LocationId},
dv=>new {bizId=dv.BusinessID,lId= dv.LocationID},
(d,dv)=>d);
if (locationId.HasValue)
query = query.Where(d => d.LocationId == locationId.Value);
return GetDealerDetails(query)
.OrderBy(d =>d.DealerName)
.ToList();
}
}
控制器动作
public ActionResult GetLocations(Guid? id)
{
IList<DealerInfo> locations;
locations = Service.GetBusinessLocations(id);
ViewData["LocationId"] = new SelectList(locations, "LocationId", "DealerName", "(Select)");
return PartialView("_DealerDropDown");
}
部分视图
<div class="editor-field" id="locationdropdown">
@Html.DropDownList("LocationId", ViewData["LocationId"] as SelectList, "(Select)", new { onchange = "onLocationSelected()", id="querydrop" })
</div>
我知道这是针对我的特定问题的,但希望类似情况下的人会发现这很有用。