0

我在访问使用 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,但我继续收到错误消息,即“参数字典包含参数 '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 上找到类似的场景。

4

1 回答 1

0

我最终对这段代码进行了很多更改,所以我用我的修复来结束这个问题。希望该方法可以帮助其他人。

服务查询

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>

我知道这对我的特定问题非常具体,但希望在类似情况下的人可能会发现这很有用。

于 2013-08-08T16:47:55.923 回答