[英]ASP.NET MVC Object reference not set to an instance of an object
I saw a similar question here on SO but I believe mine differs a little a bit. 我在SO上也看到了类似的问题 ,但我相信我的观点有所不同。
OK, so I have a simple view here: 好的,所以我在这里有一个简单的视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RootFinder.Models.QuadCalc>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Polynomial Root Finder - Quadratic
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Quadratic</h2>
<%= Html.BeginForm("Quadratic", "Calculate") %>
<% { %>
<div>
a: <%= Html.TextBox("quadAValue", Model.quadraticAValue) %>
<br />
b: <%= Html.TextBox("quadBValue", Model.quadraticBValue) %>
<br />
c: <%= Html.TextBox("quadCValue", Model.quadraticCValue) %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<p><%= Model.x1 %></p>
<p><%= Model.x2 %></p>
</div>
<% } %>
</asp:Content>
And my controller here: 我的控制器在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RootFinder.Models;
namespace RootFinder.Controllers
{
public class CalculateController : Controller
{
//
// GET: /Calculate/
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
return View(newQuadCalc);
}
public ActionResult Cubic()
{
return View();
}
public ActionResult Quartic()
{
return View();
}
}
}
Now, upon loading my Get
version of the Quadratic
view, I get the following message from VS2010: 现在,在加载我的
Quadratic
视图的Get
版本时,我从VS2010得到以下消息:
Object reference not set to an instance of an object
Now, I kind of understand the message in it of itself, but isn't it a bad thing to create a new object of a class within the View itself? 现在,我有点了解消息本身的含义,但是在View本身中创建类的新对象不是一件坏事吗? Which is why I was trying to handle this in the Controller for the
Post
only..... 这就是为什么我试图仅在
Post
Controller中处理此问题的原因.....
Hmm... 嗯...
Same as you do in your Post action, but in the Get you pass a new fresh initialized QuadCalc model to the view 与在Post操作中一样,但是在Get中,将新的初始化过的QuadCalc模型传递给视图
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic() {
return View( new QuadCalc() );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.