2013-02-14 59 views
0

我有一些麻煩訪問使用一個GUID參數,生成.ToList()下拉使用的IList需要的Guid

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); 
    } 

在我看來,我只需調用dropdownlistfor

@Html.DropDownListFor(m =>m.BusinessId, (IEnumerable<SelectListItem>)ViewBag.Locations,"Select your Businesses Location") 

如果有人可以提供一些見解,我將不勝感激。我很難在SO上找到類似的場景。

+0

您的.Join是否生成空指針?我在想可能有一個空值返回到非1:1映射值。如果這可能是這種情況,我可能會嘗試執行一個可爲空的,這可能會解決您遇到的錯誤? – Magnum 2013-02-14 22:13:49

+0

我不認爲這是問題,在連接中不應該有任何空值返回。另外,當我使用我們的WCF測試客戶端時,我可以手動輸入buisnessId的Guid值,以返回我需要的數據。 – WiseGuy 2013-02-14 22:24:10

回答

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> 

我知道這是非常具體的,以我的具體問題,但希望有人在類似情況下可能找到這個有用的。