MVC3 VB(非C#)级联下拉菜单-国家(地区) [英] MVC3 VB (not C#) Cascading DropDown - Country to state

查看:71
本文介绍了MVC3 VB(非C#)级联下拉菜单-国家(地区)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手,我觉得我觉得很简单,但遇到了麻烦.

2个下拉菜单(国家和州),选择一个国家后,该国家/地区的州/省便会被过滤到另一个下拉菜单中.

我没有看到很多示例,但是都无济于事,因为我使用以下代码构建了应用程序:
-MVC数据库优先(所以我只有.edmx而没有ModelViews)
-VB(大多数示例是C#,在它们之间进行转换真的很糟糕)

我可以生成包括带有单个下拉菜单的工作视图在内的所有内容,我只需要一个JQuery或AJAX解决方案即可根据选择进行过滤.

请分步说明或详细的代码示例.

I am a newbie with MVC and I am having trouble with what I thought was something simple.

2 drop downs (Country and State), where once a country is selected, the states/Providences for that country will be filtered into the other drop down.

I have see na lot of examples, however none are helping because I have built my application using:
-MVC Database First (so I only have the .edmx and not ModelViews)
-VB (most examples are in C# and I am really bad a converting between them)

I can generate everything including a working view with the individual dropdowns, I just need a JQuery or AJAX solution to filter based on selection.

Step-by-step instructions or detailed code examples, please.

推荐答案

我在Google中找到了这些,请继续
将CascadingDropDown与数据库一起使用-VB.NET/ASP.NET [ ^ ]
MVC:使用AJAX和jQuery的级联下拉菜单-VB.Net版本 [
I found these in Google, go ahead
Using CascadingDropDown with a Database - VB.NET/ASP.NET[^]
MVC: A Cascading Dropdown using AJAX and jQuery - VB.Net Version[^]


我已尝试转换Stephen Walther选项1,客户端从他的提示#41 .我认为我已经很好地将其转换为VB,除了我认为我对助手的处理是错误的.我添加了名称空间< add namespace ="System.Web.Helpers">在我的web.config中.

我收到以下编译错误:

``CascadingDropDownList''不是``System.Web.Mvc.HtmlHelper(Of PFS.KF_Address)''的成员
任何人都知道如何解决这个问题,或者我应该在VB中的何处/如何导入正确的名称空间?

这是我的AddressControl.vb文件的简化版本:
I have tried to convert Stephen Walther Option 1, client side from his Tip #41. I think that I have converted things to VB well enough, except that I think I did the helper wrong. I have added the namespace <add namespace="System.Web.Helpers"> in my web.config.

I am getting the following compile error:

''CascadingDropDownList'' is not a member of ''System.Web.Mvc.HtmlHelper(Of PFS.KF_Address)''

Anyone know how to resolve this or where/how in VB I should import the correct namespace?

Here is a shortened version of my AddressControl.vb file:
Imports System.Data.Entity
Imports System.Web.Mvc
Imports System.Linq
Imports PFS.PFS.Helpers

Namespace PFS
    Public Class AddressController
        Inherits System.Web.Mvc.Controller

        ' GET: /Address/Create

        Function Create() As ViewResult
            ViewBag.CountryID = New SelectList(db.KF_Countries, "ID", "Country")
            ViewBag.StateID = New SelectList(db.KF_States, "ID", "State")
            ViewBag.TypeID = New SelectList(db.KF_AddressTypes, "ID", "Name")

            ViewData("CountryList") = New SelectList(db.KF_Countries, "ID", "Country")
            ViewData("StateList") = New CascadingSelectList(db.KF_States, "Country_ID", "ID", "State")
            Return (View())
        End Function

    End Class
End Namespace



我使用的是_Layout,而不是site.master.



I am using the _Layout and not the site.master.

@Imports PFS.PFS.Helpers
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewData("Title")</title>
    
    
    

</head>
<body>
    <div class="page">
        <header id="Head1">
            <div id="title">
                <h1>The PFS Application</h1>
            </div>

            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            
            
           <nav>
                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Maintenance", "Maintenance", "Home")</li>
                </ul>

            </nav>



        </header>


        <section id="main">
            <div id="MainContent">
            @RenderBody()
            
            </div>
        </section>
        <footer>
        </footer>
    </div>
</body>
</html>



这是我的视图:Create.VBHtml



Here is my View: Create.VBHtml

@ModelType PFS.KF_Address

@Code
    ViewData("Title") = "Create"
End Code

<h2>Create</h2>


<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js") type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/CascadingDropDownList.js") type="text/javascript"></script>

@Using Html.BeginForm()
    @Html.ValidationSummary(True)
    @<fieldset>
        <legend>KF_Address</legend>
        <div>
            <label for="Countries">
                Countries:</label>
            @Html.DropDownList("--Select Country--", "CountryList")   
            <label for="States">
                States:</label>
            @Html.CascadingDropDownList("StateList", "CountryList")
        </div></fieldset>



这是我的助手:JavaScriptExtensions.vb



Here is my Helper: JavaScriptExtensions.vb

Imports System.Collections
Imports System.Collections.Generic
Imports System.Web.Mvc
Imports System.Web.UI
Imports System.Text



Namespace PFS.Helpers
    Public NotInheritable Class JavaScriptExtensions
        Private Sub New()
        End Sub

        Public Shared Function CascadingDropDownList(helper As HtmlHelper, name As String, associatedDropDownList As String) As String
            Dim sb = New StringBuilder()

            ' render select tag
            sb.AppendFormat("<select name="{0}" id="{0}"></select>", name)
            sb.AppendLine()

            ' render data array
            sb.AppendLine("<script type="text/javascript">")
            Dim data = DirectCast(helper.ViewDataContainer.ViewData(name), CascadingSelectList)
            Dim listItems = data.GetListItems()
            Dim colArray = New List(Of String)()
            For Each item In listItems
                colArray.Add([String].Format("{{key:'{0}',value:'{1}',text:'{2}'}}", item.Key, item.Value, item.Text))
            Next
            Dim jsArray = [String].Join(",", colArray.ToArray())
            sb.AppendFormat("


get('{0}').allOptions = [{{1}];",名称,jsArray) sb.AppendLine() sb.AppendFormat("
get('{0}').allOptions=[{1}];", name, jsArray) sb.AppendLine() sb.AppendFormat("


这篇关于MVC3 VB(非C#)级联下拉菜单-国家(地区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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