如何使用静态项目创建ASP.NET MVC HTML.DropDownList? [英] How to Create an ASP.NET MVC HTML.DropDownList with Static Items?

查看:99
本文介绍了如何使用静态项目创建ASP.NET MVC HTML.DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含2项一个DropDownList:部件名称和 NSN

I would like to create a DropDownList containing 2 items: Part Name and NSN.

这DropDownList的将被作为一个搜索框控件,这将是部分呈现为我的母版页的一部分的一部分。用户将输入搜索文本并从DropDownList中选择部分名称或NSN,并点击提交。查询将返回基于SEARCHTEXT结果。我已经定义PartsController这里是它的相关部分:

This dropdownlist will be used as part of a search box control that will be a partial render as part of my master page. The user will enter their search text and select either Part Name or NSN from the dropdownlist and click submit. The query will return results based on the searchtext. I have defined PartsController and here's the pertinent portion of it:

    Function Search(ByVal searchtext As String, ByVal SearchType As String) As ActionResult
        Dim searchlist As List(Of String) = New List(Of String)
        searchlist.Add("Part Name")
        searchlist.Add("NSN")
        ViewData("searchlist") = New SelectList(searchlist)

        If SearchType = "PARTNAME" Then
            Dim SearchResult = From p In _entities.PartList _
              Where p.PARTNAME = searchtext _
              Select p
            Return View(SearchResult)
        End If
        If SearchType = "NSN" Then
            Dim SearchResult = From p In _entities.PartList _
              Where p.NSN = searchtext _
              Select p
            Return View(SearchResult)
        End If
        Return View("UnknownType")
    End Function

PartsForm.ascx定义如下:

PartsForm.ascx is defined as follows:

    <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of DielToolMVC.PartList)" %>
    <%=Html.ValidationSummary("Please correct the errors and try again")%>
    <%  Using (Html.BeginForm("Search", "PartsController"))%>
        <fieldset>
            <p>
                <label for="Parts">Please enter a part description or NSN.</label>
                <%=Html.TextBox("searchtext") %>
                <%=Html.DropDownList("searchlist")%>
                <%=Html.ValidationMessage("Part Name or NSN", "*")%>
            </p>
            <p>
                <input type="submit" value="Search" />
            </p>
        </fieldset>
    <% End Using%>

当我调试,我收到以下错误信息:

When I debug, I receive the following error message:

有与类型的键'searchlist'的IEnumerable'。没有ViewData的项目

There is no ViewData item with the key 'searchlist' of type 'IEnumerable'.

我有点困惑,因为在 MSDN文档表明类似的例子。但是,随着这样的例子后,我得到这个错误。我是什么俯瞰?

I'm a bit confused as the MSDN documentation demonstrates similar examples. However, after following such examples I get this error. What am I overlooking?

推荐答案

如果此控件呈现为你的母版页的一部分,那么静态值列表将被添加到的ViewData在每一个操作方法将渲染与母版页视图。如果值是静态的,并不会在每次调用改变,那么你应该只是code他们进入你的局部视图是这样的:

If this control is rendered as part of your master page then the list of static values will have to be added to the ViewData in each and every action method that will render a view with that master page. If the values are static and will not change on each call then you should just code them into you partial view like this:

<select id="searchlist" name="searchlist">
        <option value="PARTNAME" label="Part Name" />
        <option value="NSN" label="NSN" />
</select>

或本

<%  Dim items As New List(Of SelectListItem)()
    items.Add(New SelectListItem() With {.Value = "PARTNAME", .Text = "Part Name", .Selected = True})
    items.Add(New SelectListItem() With {.Value = "NSN", .Text = "NSN"})
%>

<%=Html.DropDownList("searchlist", items)%>

这篇关于如何使用静态项目创建ASP.NET MVC HTML.DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆