与DropDownListFor混淆<>

时间:2010-11-17 16:58:38

标签: asp.net-mvc-2 html.dropdownlistfor

所以我要做的是将数据从数据库导入MVC2 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?

DB也可以通过guildEntities访问。该表是TBLCharS。该表中所需的字段是CharS_id和CharS_name。

1 个答案:

答案 0 :(得分:1)

首先要做的是定义你的模型。除了在模型中传递的数据之外,视图不需要任何其他数据。在调用视图之前构建模型并填充它。

我的实施有点矫枉过正,但事实如下:

在模型中,创建一个容器来保存下拉数据:

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

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

model.LocationList = repository.GetLocationsSelectList(selectedLocationId);

我使用存储库和Linq从数据库中获取数据:

        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)%>

如果您有任何疑问或需要更多代码,请添加评论。