我设置了一个简单的 MVC (RC1) 应用程序,但我看到了一些奇怪的行为。Home/Index 页面使用 ListView 显示项目列表。这是 HomeController 代码:
Function Index()
ViewData("results") = From m In context.MyTable
Return View()
End Function
Home/Index.aspx 页面上只有一个 ListView,后面的代码是这样的:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MyListView.DataSource = ViewData("results")
MyListView.DataBind()
End Sub
这在导航到主页/索引时工作正常。但是,我有另一个名为 Form 的视图和控制器。它现在只是一个存根,所以这里是 FormController:
Function Index()
Return View()
End Function
Form/Index.aspx 后面没有代码 - 再次,只是一个存根。
我看到的问题是,当我尝试导航到表单/索引时,我得到“对象引用未设置为对象的实例”。在 Home/Index.aspx.vb 后面的代码上。为什么是这样?我正试图离开该页面 - 为什么它试图执行后面的代码?如果我像这样包装代码:
If ViewData("results") IsNot Nothing Then
MyListView.DataSource = ViewData("results")
MyListView.DataBind()
End If
一切正常,但似乎我不应该这样做。我错过了什么吗?
更新:根据请求,这是 Form/Index.aspx 的内容:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="ProviderFeedback.Index" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h3>
Enter Provider Feedback
</h3>
<form method="post" action="/Form/CreateNew">
<%=Html.TextBox("member")%>
<input type="submit" value="Submit" />
</form>
</asp:Content>