调用Controller类并传递值

时间:2012-03-14 21:25:32

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing

我正在尝试使用<ui><li></li></ui>标签创建菜单。我正在读取XML文件并将其存储在List(菜单)对象中。

菜单对象:

Public Class MenuVM

#Region "Properties"
    Public Property ID As Integer
    Public Property Description As String
    Public Property Comments As String
    Public Property UserControlName As String
    Public Property AssemblyName As String
    Public Property ModelName As String
    Public Property SortOrder As Integer
    Public Property IsSelected As Boolean
#End Region

End Class

HomeController类:

    Public Class HomeController
    Inherits System.Web.Mvc.Controller


    Function Index() As ActionResult
        Dim oMenuHelper As New MenuHelper
        Return View("index", oMenuHelper.BuildMenu())
    End Function

    <HttpPost()> _
    Function Index(ByVal iSelect As Integer) As ActionResult
        Dim oMenuHelper As New MenuHelper
        Return View("index", oMenuHelper.BuildMenu())
    End Function

    Function About() As ActionResult
        Return View()
    End Function
End Class

ASPX PAGE:

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of List(OF MVCApp.MenuVM))" %>
<%@ Import Namespace ="MVCApp" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

    <div id="menu" class="block"> 
    <% Using Html.BeginForm("Index", "Home")%>  
      <ul id="menuItems"> 

        <% For Each item As MVCApp.MenuVM In Model%>
         <li><%: Html.ActionLink(item.Description, "Index", "Home", New With {item.ID}, Nothing)%></li>               
        <%Next%>
       </ul> 
        <% End Using %>              

</div> 
</asp:Content>

输出:

enter image description here

单击视图源时,输出如下:

<!DOCTYPE html>
<html>
<head><title>

    Home Page

</title><link href="Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>
            <div id="logindisplay">


        [ <a href="/Account/LogOn">Log On</a> ]

            </div> 
            <div id="menucontainer">
                <ul id="menu">
                    <li><a href="/">Home</a></li>
                    <li><a href="/Home/About">About</a></li>
                </ul>
            </div>
        </div>
        <div id="main">


    <div id="menu" class="block"> 
    <form action="/" method="post">  
      <ul id="menuItems"> 


         <li><a href="/Home/Index/1">Ballot Language</a></li>               

         <li><a href="/Home/Index/2">Assisting Absentee Voter</a></li>               

         <li><a href="/Home/Index/3">Instructions</a></li>               

         <li><a href="/Home/Index/4">Ballot Questions</a></li>               

       </ul> 
        </form>              

</div> 

            <div id="footer">
            </div>
        </div>
    </div>
</body>
</html>

错误:当我点击链接时,我收到以下错误:

enter image description here

任何人都可以帮忙吗?我的目的是将菜单ID传递给控制器​​并加载适当的用户控件。我不能在这个项目中使用Javascript或jquery,因为用户可能在机器上禁用了javascript。

2 个答案:

答案 0 :(得分:1)

尝试链接为/ Home / Index / 1 not / HomeController / Index / 1

答案 1 :(得分:1)

您应该从链接中删除“控制器”字样。网址应为:“/ Home / Index / 1”。 为此,请修改ActionLink代码:

Html.ActionLink(item.Description, "Index", "Home", New With {item.ID}, Nothing)%>