查看未将dropdownlist选择的值绑定回Model

时间:2012-05-16 20:11:41

标签: asp.net-mvc-3 razor

我正在寻找一种方法将模型中的枚举绑定到下拉列表。我找到this帖子并使用了第二个答案的代码,它在创建下拉列表时效果很好。但是,当我提交表单时,它总是使用枚举的第一个值传回模型。

枚举(这包含在我的模型中):

public LayoutType Layout;
public enum LayoutType
{
    Undefined = 0,
    OneColumn = 1,
    TwoColumn = 2,
    ThreeColumn = 3
}

Html助手方法:

private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
    {
        Type realModelType = modelMetadata.ModelType;

        Type underlyingType = Nullable.GetUnderlyingType(realModelType);
        if (underlyingType != null)
        {
            realModelType = underlyingType;
        }
        return realModelType;
    }

    private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = "", Value = "" } };

    public static string GetEnumDescription<TEnum>(TEnum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if ((attributes != null) && (attributes.Length > 0))
            return attributes[0].Description;
        else
            return value.ToString();
    }

    public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
    {
        return EnumDropDownListFor(htmlHelper, expression, null);
    }

    public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        Type enumType = GetNonNullableModelType(metadata);
        IEnumerable<TEnum> values = Enum.GetValues(enumType).Cast<TEnum>();

        IEnumerable<SelectListItem> items = from value in values
                                            select new SelectListItem
                                            {
                                                Text = GetEnumDescription(value),
                                                Value = value.ToString(),
                                                Selected = value.Equals(metadata.Model)
                                            };

        // If the enum is nullable, add an 'empty' item to the collection
        if (metadata.IsNullableValueType)
            items = SingleEmptyItem.Concat(items);

        return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
    }

查看:

@Html.EnumDropDownListFor(model => model.Layout)

在渲染视图时,下拉列表按预期完全填充,并选择正确的值。但是,当我提交POST并将值传递回控制器时,Model.Layout的值始终为“Undefined”。任何帮助将不胜感激!谢谢,

2 个答案:

答案 0 :(得分:1)

如果您使用的是VB.Net - MVC4- Razor 这个答案与Frigik(Thanks Frigik)几乎相同

在你的模型中创建两个字段,一个是保存SelectedItem的字段(这里是字符串的TagType),第二个是下拉值(TagTypeList)

Model Class-Tag.vb

Imports System.Web.Mvc

Private _tagType As String
Private _tagTypeList As List(Of SelectListItem)

Public Property TagType() As String
        Get
            Return _tagType
        End Get
        Set(ByVal value As String)
            _tagType = value
        End Set
End Property

Public Property TagTypeList() As List(Of SelectListItem)
        Get
            Return _tagTypeList
        End Get
        Set(value As List(Of SelectListItem))
            _tagTypeList = value
        End Set
    End Property

'In the constructor

Sub New()
        TagTypeList = CommonUtility.LoadDropDownByName("TAGTYPE")
End Sub

在CommonUtility类中 - CommonUtility.vb

Imports System.Web.Mvc
Imports System.Collections.Generic


Public Shared Function LoadDropDownByName(ByVal DropDownName As String) As List(Of SelectListItem)
        Dim dt As DataTable
        Dim ds As DataSet
        Dim results As List(Of SelectListItem) = Nothing
        Try
            ds = obj.LoadDropDown(DropDownName)   'Pass the dropdown name here and get the values from DB table which is - select ddlId, ddlDesc from <table name>
            If Not ds Is Nothing Then
                dt = ds.Tables(0)
                If (dt.Rows.Count > 0) Then
                    results = (From p In dt Select New SelectListItem With {.Text = p.Item("ddlDesc").ToString(), .Value = p.Item("ddlId").ToString()}).ToList()
                End If
            End If
        Catch ex As Exception
        End Try
        Return results
    End Function

在视图中

@Html.DropDownListFor(Function(x) x.TagType, Model.TagTypeList, "", New With {.id = "ddlTagType",.class = "dropDown", .style = "width: 140px"})

这里第三个参数(可选)为空,它将在下拉列表中将第一个项目作为空白插入。第一个参数是selectedItem,它有助于在DB表中已存在值后填充。

希望这有助于谁使用VB.Net

答案 1 :(得分:0)

在你的模型中创建两个字段,一个应该保存所选项目(SelectedItem)第二个下拉值(GroupNames)

  public string SelectedItem { get; set; }

  [Required]
  public IEnumerable<SelectListItem> GroupNames { get; set; }

然后在构造函数中填写GroupNames,例如

GroupNames = layer.GetAll().Select(p => new SelectListItem
                                                        {
                                                            Value = p.Id.ToString(CultureInfo.InvariantCulture),
                                                            Text = p.Name,
                                                            Selected = p.Id == someEntity.someFK
                                                        });
            SelectedItem = GroupNames.Where(p => p.Selected).Select(p => p.Value).Single();

然后在视图渲染中下拉:

 @Html.DropDownListFor(x => x.SelectedItem, Model.GroupNames)

这是所有选定的值应该与SelectedItem字段中的模型一起提供。