我是一名.NET和MVC新手,并且在使用ASP经历了太长时间之后第一次学习它,现在是时候我转换为更容易构建Web应用程序的工作了。
我一直在阅读Stephen Walther的有用的视频教程,以了解大多数事情,到目前为止我取得了很好的进展。我来的地方是在我的应用程序ConversationApp中创建记录的详细信息页面。列出数据和插入新数据是完美的,但每次我去查看任何特定记录的详细信息页面时,我都会收到错误消息“对象引用未设置为对象的实例”。
传递给控制器的URL是: / Home / Details / X(其中X是记录的唯一ID)
我很感激任何帮助让我过去可能是我的一个非常愚蠢的错误或疏忽。
这是我到目前为止的代码:
HomeController.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ConversationApp.Models;
namespace ConversationApp.Controllers
{
public class HomeController : Controller
{
private conversationDBEntities _entities = new conversationDBEntities();
//
// GET: /Home/
public ActionResult Index()
{
return View(_entities.conversation.ToList());
}
//
// GET: /Home/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Home/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Home/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="conversation_id")]conversation conversationToCreate)
{
try
{
// TODO: Add insert logic here
_entities.AddToconversation(conversationToCreate);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Home/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Details.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ConversationApp.Models.conversation>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Details</h2>
<fieldset>
<legend>Fields</legend>
<p>
conversation_name:
<%= Html.Encode(Model.conversation_name) %>
</p>
<p>
conversation_owner:
<%= Html.Encode(Model.conversation_owner) %>
</p>
<p>
conversation_description:
<%= Html.Encode(Model.conversation_description) %>
</p>
<p>
conversation_homeurl:
<%= Html.Encode(Model.conversation_homeurl) %>
</p>
</fieldset>
<p>
<%=Html.ActionLink("Edit", "Edit", new { id=Model.conversation_id }) %> |
<%=Html.ActionLink("Back to List", "Index") %>
</p>
/ Home / Details / X的错误消息(其中X是记录的唯一ID)
Server Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 13: <p>
Line 14: conversation_name:
Line 15: <%= Html.Encode(Model.conversation_name) %>
Line 16: </p>
Line 17: <p>
Source File: c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx Line: 15
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.views_home_details_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx:15
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Shared\Site.Master:26
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4918; ASP.NET
Version:2.0.50727.4918
答案 0 :(得分:1)
在控制器上的“详细信息”操作方法中,您没有将模型传递回视图。
public ActionResult Details(int id)
{
// get model object
return View(//pass model object to view here);
}
答案 1 :(得分:1)
您似乎没有将任何数据传递到Details
视图:
public ActionResult Details(int id)
{
return View();
}
从存储库中检索模型类并将其传递给视图:
return View(conversation);
答案 2 :(得分:0)
你的问题在
return View(_entities.conversation.ToList());
您的视图只是您的页面。 View()通常是您要显示的页面的路径。如果您在没有参数的情况下传递它,ASP将根据您的控制器获得默认路由,在这种情况下,它将被重定向到Home / Index.aspx或Default.aspx,具体取决于您的路由选项。
如果要将数据传递到视图,则应使用ViewData哈希。该哈希值在控制器和页面端可用。
例如:
Controller Code:
public ActionResult Index()
{
List<string> li = new List<string>();
li.Add("test");
ViewData["myList"] = li;
return View();
}
Page Code:
<div>
<%
List<string> li = (List<string>)ViewData["myList"];
foreach(string str in li)
{
%>
<p><%= str %></p>
<%
}
%>
</div>
答案 3 :(得分:0)
感谢大家的超级帮助,这些提示使我指向了正确的方向。我错误地认为HomeController足够聪明,可以在创建视图后确定我想要它显示的内容。
这些线索引导我: http://www.asp.net/learn/MVC/tutorial-26-cs.aspx
经过一些阅读后,我找到了我想要的答案。
以下是 HomeController.CS 的具体部分,其中展示了我如何运作:
// GET: /Home/Details/5
public ActionResult Details(int id)
{
var conversationToView = (from c in _entities.conversation
where c.conversation_id == id
select c).FirstOrDefault();
return View(conversationToView);
}
正如阿赫里克所说,我没有把模型传回去。 conversationToView获取您要查看的详细信息的记录ID,并将该信息传递给返回视图操作以检索所需结果。
与此同时,由于上述反馈,我也能够从我找到的教程中得到正确执行编辑的答案。它仍然没有提交编辑过的信息,而是一次一步。
再次感谢所有对此做出快速反应的人。它已经绞尽脑汁好几天了。