ASP.NET 二次计算错误地返回 NaN
我的 ASP.NET MVC Web 应用程序应该计算二次多项式(二次)的根,但我错误地得到了 Nan
响应。我相信这是由于我的设置不当造成的,所以让我在这里发布一些代码:
视图:
<%@ 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>
<% using(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 />
<br />
<strong>Root 1:</strong>
<p><%= Model.x1 %></p>
<br />
<strong>Root 2:</strong>
<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();
}
[HttpGet]
public ViewResult Quadratic()
{
return View(new QuadCalc());
}
[HttpPost]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
newQuadCalc.QuadCalculate();
return View(newQuadCalc);
}
public ActionResult Cubic()
{
return View();
}
public ActionResult Quartic()
{
return View();
}
}
}
模型(执行计算):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RootFinder.Models
{
public class QuadCalc
{
public double quadraticAValue { get; set; }
public double quadraticBValue { get; set; }
public double quadraticCValue { get; set; }
public double x1 { get; set; }
public double x2 { get; set; }
public void QuadCalculate()
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//No real solution
x1 = double.NaN;
x2 = double.NaN;
}
else if (insideSquareRoot == 0)
{
//One Solution
double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtOneSolution) / (2 * quadraticAValue);
x2 = double.NaN;
}
else if (insideSquareRoot > 0)
{
//Two Solutions
double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtTwoSolutions) / (2 * quadraticAValue);
x2 = (-quadraticBValue - sqrtTwoSolutions) / (2 * quadraticAValue);
}
}
}
}
所以,让我们在视图中说,最终用户输入 a=1
、b=0
和 c=0
,我的应用程序将输出 Nan
对于 Root1 和 Root2。我认为这可能是由于我没有将最终用户输入正确连接到 newQuadCalc 对象的实例变量中......
My ASP.NET MVC web application is supposed to calculate the roots of a second-degree polynomial (quadratic) but I'm erroneously getting Nan
responses. I believe this is due to my improper setup, so let me post some of my code here:
View:
<%@ 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>
<% using(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 />
<br />
<strong>Root 1:</strong>
<p><%= Model.x1 %></p>
<br />
<strong>Root 2:</strong>
<p><%= Model.x2 %></p>
</div>
<% } %>
</asp:Content>
Controller (handles all controlling of requests for calculations like the quadratic):
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();
}
[HttpGet]
public ViewResult Quadratic()
{
return View(new QuadCalc());
}
[HttpPost]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
newQuadCalc.QuadCalculate();
return View(newQuadCalc);
}
public ActionResult Cubic()
{
return View();
}
public ActionResult Quartic()
{
return View();
}
}
}
Model (performing calculation):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RootFinder.Models
{
public class QuadCalc
{
public double quadraticAValue { get; set; }
public double quadraticBValue { get; set; }
public double quadraticCValue { get; set; }
public double x1 { get; set; }
public double x2 { get; set; }
public void QuadCalculate()
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//No real solution
x1 = double.NaN;
x2 = double.NaN;
}
else if (insideSquareRoot == 0)
{
//One Solution
double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtOneSolution) / (2 * quadraticAValue);
x2 = double.NaN;
}
else if (insideSquareRoot > 0)
{
//Two Solutions
double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrtTwoSolutions) / (2 * quadraticAValue);
x2 = (-quadraticBValue - sqrtTwoSolutions) / (2 * quadraticAValue);
}
}
}
}
So, let's say in the View, the end-user enters a=1
, b=0
and c=0
, my application will output Nan
for both Root1 and Root2. I think this may be due to the fact that I have not properly connected my end-user input into the instance variables of my newQuadCalc object...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码失败,因为在网站上输入的值不会传播到您的计算逻辑。发生这种情况是因为文本框名称与模型属性名称不匹配导致模型绑定失败。你的观点应该是这样的:
Your code is failing because the values that get entered on the website are not propagated to your calculation logic. This is happening because model binding is failing due to a mismatch between the textbox names and the names of your model's properties. Your view should instead be something like this: