如何在没有ViewData的情况下绑定Html.DropDownList(强类型视图)

时间:2009-05-21 02:08:28

标签: asp.net-mvc drop-down-menu strongly-typed-view

我似乎无法找到一篇好的博客文章,其中展示了如何将模型绑定到视图而没有魔术字符串“ViewData”(使用强类型视图是我正在尝试的方法)

有没有人知道我需要在下面改变什么来直接将它绑定到我的模型?

查看

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of MvcApplication1.Category))" %>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <%=Html.DropDownList("CategoryList")%>
</asp:Content>

控制器

Function Index() As ActionResult
    Dim list As New List(Of Category)
    list.Add(New Category With {.CategoryID = 1, .CategoryName = "Test1"})
    list.Add(New Category With {.CategoryID = 2, .CategoryName = "Test2"})

    Return View()
End Function

修改

VB中的最终解决方案如下所示,感谢您的回复!

控制器

Function Index() As ActionResult
    Dim id As Integer = 1
    Dim ProductObject As Product = mProductService.GetProductById(id)

    Return View(ProductObject)
End Function

查看

<%=Html.DropDownList("Category", New SelectList(Model.Categories, "CategoryID", "CategoryName"))%>

产品类别(带有类别的IEnumeralbe属性)

Public Class Product
    Public Sub New()

    End Sub


    Private mProductID As Integer
    Public Property ProductID() As Integer
        Get
            Return mProductID
        End Get
        Set(ByVal value As Integer)
            mProductID = value
        End Set
    End Property

    ReadOnly Property Categories() As IEnumerable(Of Category)
        Get
            Dim list As New List(Of Category)
            list.Add(New Category With {.CategoryID = 1, .CategoryName = "Test1"})
            list.Add(New Category With {.CategoryID = 2, .CategoryName = "Test2"})

            Return list
        End Get
    End Property

End Class

3 个答案:

答案 0 :(得分:5)

下拉列表帮助器需要IEnumerable<SelectListItem>,而不是IEnumerable<Category>。通常,您所做的是让您的页面具有特定的模型。该模型包含下拉列表中所选值的属性,而不是集合。您提供的集合可通过ViewData进行选择。您可以拥有一个仅包含视图的模型,包括要从中选择的属性和集合,但这可能意味着类的扩散。关于增殖类或魔术弦是否设计更糟,存在一些争论。

我对你的代码的看法是:

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of Foo)" %>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <%=Html.DropDownList("Category", ViewData("CategoryList"))%>
</asp:Content>

Function Index() As ActionResult
    Dim list As New List(Of SelectListItem)
    list.Add(New SelectListItem  With {.Value = 1, .Text = "Test1"})
    list.Add(New SelectListItem With {.Value = 2, .Text = "Test2"})
    ViewData("CategoryList") = list
    Return View()
End Function

其中Foo是一个包含int类型属性Category的类。

如果你想做一个强类型的视图,那么让Foo有一个类型为SelectListItem的属性,然后改变:

<%=Html.DropDownList("Category", ViewData("CategoryList"))%>

<%=Html.DropDownList("Category", Foo.Categories )%>

    ViewData("CategoryList") = list
    Return View()

    Dim foo as New Foo
    foo.Categories = list
    Return View(foo)

答案 1 :(得分:1)

您可以为Html.DropDownList创建自己的帮助器方法重载,直接将模型映射到DropDownList帮助器。

话虽如此,你应该问自己,你的观点应包含多少域特定逻辑。 ViewData有助于将您的模型和域逻辑与您的视图分开。

示例:

我知道这不是一个“重载”因为觉得因为这仍然需要一个名称,你会想要另一个方法,只需要一个字符串作为下拉名称,但自动将模型绑定到控件(假设它是一个IEnumerable)。这是HtmlHelper扩展方法的一个示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace MvcApplication3.Helpers
{
    public static class ViewHelpers
    {
        public static string ModelDropDownList(this HtmlHelper htmlHelper, string name)
        {
            var model = htmlHelper.ViewData.Model as IEnumerable<SelectListItem>;
            if(model == null)
                return string.Empty;

            return htmlHelper.DropDownList(name, model);
        }
    }
}

以下是您在视图中调用此内容的方法

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SelectListItem>>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.ModelDropDownList("Test") %>
</asp:Content>

答案 2 :(得分:0)

如果你想使用Strongly Typed方法,你可以传递一个可以传递IEnumerable的SelectList对象。您必须指定T的文本字段和值字段属性,但此SelectList对象可以直接分配给Html.DropDownList帮助程序:

c#
IList<Category> categories = new IList<Category>()
    {
        new Category() { ID = 1, Name = "Category 1" },
        new Category { ID = 2, Name = "Category 2" }
    };

SelectList categoryDropDown = new SelectList(categories, "ID", "Name");

ViewData["categoryDropDown"] = categoryDropDown;

然后做:

c#
<%=Html.DropDownList("CategoryID", ViewData("categoryDropDown"))%>

通过这种方式,您可以从任何地方传递IList类别(服务层,控制器范围方法)。