我的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>
Controller(处理对二次方计算请求的所有控制):
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
,我的应用程序将为Root1和Root2输出Nan
。我想这可能是因为我没有将我的最终用户输入正确地连接到我的newQuadCalc对象的实例变量...
答案 0 :(得分:2)
您的代码失败,因为在网站上输入的值不会传播到您的计算逻辑。发生这种情况是因为模型绑定由于文本框名称与模型属性名称不匹配而失败。您的观点应该是这样的:
<% using (Html.BeginForm("Quadratic", "Calculate")) %>
<% { %>
<div>
a: <%= Html.TextBoxFor(m=> m.quadraticAValue) %>
<br />
b: <%= Html.TextBoxFor(m => m.quadraticBValue) %>
<br />
c: <%= Html.TextBoxFor(m => m.quadraticCValue)%>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<strong>Root 1:</strong>
<p><%= Html.DisplayFor(m => m.x1) %></p>
<br />
<strong>Root 2:</strong>
<p><%= Html.DisplayFor(m => m.x2)%></p>
</div>
<% } %>