ASP.NET MVC 对象引用未设置为对象的实例
我看到了 类似的问题在这里,但我相信我的有点不同。
好的,我这里有一个简单的视图:
<%@ 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>
我的控制器在这里:
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();
}
}
}
现在,在加载我的 Quadratic
视图的 Get
版本后,我从 VS2010 收到以下消息:
< code>对象引用未设置为对象的实例
现在,我有点理解它本身的消息了,但是在视图本身中创建类的新对象不是一件坏事吗?这就是为什么我试图在控制器中仅针对 Post
处理这个问题......
嗯......
I saw a similar question here on SO but I believe mine differs a little a bit.
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:
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? Which is why I was trying to handle this in the Controller for the Post
only.....
Hmm...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与您在 Post 操作中所做的相同,但在 Get 中将新的初始化的 QuadCalc 模型传递给视图
Same as you do in your Post action, but in the Get you pass a new fresh initialized QuadCalc model to the view