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

发布于 2024-09-29 10:33:05 字数 2347 浏览 5 评论 0原文

我看到了 类似的问题在这里,但我相信我的有点不同。

好的,我这里有一个简单的视图:

<%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-10-06 10:33:05

与您在 Post 操作中所做的相同,但在 Get 中将新的初始化的 QuadCalc 模型传递给视图

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic() {
    return View( new QuadCalc() );
}

Same as you do in your Post action, but in the Get you pass a new fresh initialized QuadCalc model to the view

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic() {
    return View( new QuadCalc() );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文