简体   繁体   English

ASP.NET MVC对象引用未设置为对象的实例

[英]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.

相关问题 Asp.Net MVC4&#39;对象引用未设置为对象的实例&#39; - Asp.Net MVC4 'Object reference not set to an instance of an object' 使用ASP.NET 5和MVC 6检查会话是否为空时,获取NullReferenceException和对象引用未设置为对象的实例 - Getting NullReferenceException and object reference not set to an instance of an object when checking if session is null with ASP.NET 5 and MVC 6 Facebook登录Asp.net Mvc对象引用未设置为对象的实例 - Facebook Login Asp.net Mvc Object reference not set to an instance of an object ASP.NET MVC“'对象引用未设置为 object 的实例。'”错误 - ASP.NET MVC "'Object reference not set to an instance of an object.'" Error Object 参考未设置为 object 的实例? (异常错误~ASP.NET MVC) - Object Reference not set to an instance of an object? (Exception error ~ ASP.NET MVC) 尝试使用 ASP.NET MVC 上传 PDF 文件导致错误“对象引用未设置为对象的实例” - Trying to upload a PDF file using ASP.NET MVC is causing an error "Object reference not set to an instance of an object" 在 ASP.NET MVC 视图(剃刀代码)中制作图表时“对象引用未设置为对象的实例” - "Object reference not set to an instance of an object" while making chart in ASP.NET MVC view (Razor code) ASP.NET MVC | DropDownListFor给出错误的对象引用未设置为对象的实例 - ASP.NET MVC | DropDownListFor gives error Object reference not set to an instance of an object “对象引用未设置为对象的实例。”嵌套列表ASP.NET MVC LINQ实体框架 - “Object reference not set to an instance of an object.” Nesting Lists ASP.NET MVC LINQ Entity Framework ASP.NET MVC3 - “对象引用未设置为对象的实例”错误 - ASP.NET MVC3 - “Object reference not set to an instance of an object” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM