2010-11-17 60 views
0

所以我想要做的是從數據庫導入數據到MVC2 DropDownListFor。與DropDownListFor <>的混亂

我基本上已經去過的角色扮演公會網站我正在爲一個朋友工作。頁面應該做的是讓一個人編輯他們的角色。所以傳入頁面的是包含字符信息的模型。然後,我有視圖調用YourCharacterEdit模型中的方法來獲取玩家角色的種族信息。

所以我試圖遵循很多不同的想法,我發現從各種教程在線,但似乎沒有任何工作。我非常樂於接受任何人在這一點上可能提出的想法,以及我如何填寫下拉列表。目前,我沒有太多的模特可供展示,因爲我一直在不停地刮掉我想出來的任何東西。

控制器:

if (Request.IsAuthenticated) 
      { 

       UserRepository _urepos = new UserRepository(); 
       CharacterRepository _crepos = new CharacterRepository(); 
       var check = _urepos.GetSpecificUserByName(User.Identity.Name); 
       var yourchar = _crepos.GetSpecificCharacter(CharID); 

       if (yourchar.CharUserRef == check.UserID) 
       { 
        // 
        YourCharacterEdit VarToReturn = new YourCharacterEdit(); 

        VarToReturn.EmployeeID = yourchar.EmployeeID; 
        VarToReturn.CharFirstName = yourchar.CharFirstName; 
        VarToReturn.CharLastName = yourchar.CharLastName; 
        VarToReturn.CharGender = yourchar.CharGender; 
        VarToReturn.CharSpecies = yourchar.CharSpecies; 
        VarToReturn.CharDescription = yourchar.CharDescription; 
        VarToReturn.CharBackground = yourchar.CharBackground; 
        VarToReturn.CharJob = yourchar.CharJob; 

        return View("CharEdit", VarToReturn); 
       } 
       else 
       { 
        return View("401"); 
       } 


      } 

的觀點:

<div class="editor-field"> 
       <%: Html.DropDownListFor(model => model.CharSpecies, ?)%> 
       <%: Html.ValidationMessageFor(model => model.CharSpecies) %> 
      </div> 

因此,任何人有獲得ListBox中傳播什麼好的方法呢?

此外,數據庫是通過公會實體訪問。該表是TBLCharS。該表內需要的字段是CharS_id和CharS_name。

回答

1

首先要做的是定義你的模型。除了在模型中傳遞的信息外,視圖不需要任何其他數據。在調用視圖之前構建模型並填充它。

我的實現是一個有點矯枉過正,但這裏有雲:

在模型中,創建一個容器來保存下拉數據:

public IEnumerable<SelectListItem> LocationList { get; set; } 

在控制器中,填充你的模型,包括下拉列表:

model.LocationList = repository.GetLocationsSelectList(selectedLocationId); 

我使用存儲庫和LINQ to抓住從數據庫中的數據:

 var q = (from l in Repository.For<LocationEntity>() 
       select new 
       { 
        RowId = l.RowId, 
        LocationString = l.Name, 
       }); 

     var result = q.ToSelectList(a => a.RowId.ToString(), a => a.LocationString, a => a.RowId == locationId); 

     return result; 

的ToSelectList擴展我從什麼地方抓起(我忘了在哪裏):

public static class EnumerableExtensions 
{ 
    /// <summary> 
    /// Converts the source sequence into an IEnumerable of SelectListItem 
    /// </summary> 
    /// <param name="items">Source sequence</param> 
    /// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param> 
    /// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param> 
    /// <returns>IEnumerable of SelectListItem</returns> 
    public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector) 
    { 
     return items.ToSelectList(valueSelector, nameSelector, x => false); 
    } 

    /// <summary> 
    /// Converts the source sequence into an IEnumerable of SelectListItem 
    /// </summary> 
    /// <param name="items">Source sequence</param> 
    /// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param> 
    /// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param> 
    /// <param name="selectedItems">Those items that should be selected</param> 
    /// <returns>IEnumerable of SelectListItem</returns> 
    public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, IEnumerable<TValue> selectedItems) 
    { 
     return items.ToSelectList(valueSelector, nameSelector, x => selectedItems != null && selectedItems.Contains(valueSelector(x))); 
    } 

    /// <summary> 
    /// Converts the source sequence into an IEnumerable of SelectListItem 
    /// </summary> 
    /// <param name="items">Source sequence</param> 
    /// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param> 
    /// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param> 
    /// <param name="selectedValueSelector">Lambda that specifies whether the item should be selected</param> 
    /// <returns>IEnumerable of SelectListItem</returns> 
    public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, Func<TItem, bool> selectedValueSelector) 
    { 
     foreach (var item in items) 
     { 
      var value = valueSelector(item); 

      yield return new SelectListItem 
      { 
       Text = nameSelector(item), 
       Value = value.ToString(), 
       Selected = selectedValueSelector(item) 
      }; 
     } 
    } 
} 

最後,在你看來:

<%: Html.LabelFor(m => m.LocationId)%> 
<%: Html.DropDownListFor(m => m.LocationId, Model.LocationList, "<-- Select One -->")%> 
<%: Html.ValidationMessageFor(m => m.LocationId)%> 

添加評論,如果您有任何疑問或需要更多碼。

+0

如果您希望允許位於下拉列表頂部的「n/a」應該對應於LocationId設置爲null,該怎麼辦? – 2011-05-13 00:15:47