好的,我有一个问题,我的计算永远不会发生,就像它应该的那样。我正在尝试根据以原始形式输入的a
,b
和c
的值,使用二次公式进行求解二次方程根的计算。
现在,让我为这个应用程序提供我的Model,view和Controller的代码:
型号:
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(out double x1, out double x2)
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate the inside of the square root
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//There is no solution
x1 = double.NaN;
x2 = double.NaN;
}
else
{
//Compute the value of each x
//if there is only one solution, both x's will be the same
double sqrt = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrt) / (2 * quadraticAValue);
x2 = (-quadraticBValue - sqrt) / (2 * quadraticAValue);
}
}
}
}
查看:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<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("quadraticAValue") %>
<br />
b: <%= Html.TextBox("quadraticBValue") %>
<br />
c: <%= Html.TextBox("quadraticCValue") %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<p><%= ViewData["Root1"] %></p>
<p><%= ViewData["Root2"] %></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()
{
ViewData["Root1"] = "";
ViewData["Root2"] = "";
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Quadratic(QuadCalc newQuadCalc)
{
ViewData["Root1"] = newQuadCalc.x1;
ViewData["Root2"] = newQuadCalc.x2;
return View();
}
public ActionResult Quartic()
{
return View();
}
public object x1 { get; set; }
public object x2 { get; set; }
}
}
现在,我正在努力解决这个问题,但我认为问题可能在于将输入值从表单分配到新的计算对象中。这也可能是一个问题,考虑到MVC可能会在表单输入字符串stype而不是double,在我的模型中,我执行基于输入的计算是双重类型。
如果有必要,我不知道在哪里将字符串转换为双倍转换。
有谁知道为什么会出现问题。
该页面输出以下内容:
x1 = 0
x2 = 0
答案 0 :(得分:4)
你永远不会调用QuadCalculate方法。
除此之外,我建议从视图模型中移出QuadCalculate。这个最新建议不会以任何方式影响实际答案。这只是实现MVC模式的一种尊重的方式。
例如:
您的模型类
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; }
}
和您的控制器方法
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Quadratic(QuadCalc newQuadCalc) {
// move your calculation method here or, better,
// to a class that implements the repository pattern
// do the calculation and then set the x1 and x2 members of
// your model class
// You dont even need to use ViewData for this, just return
// back your model class to your view
return View( newQuadCalc );
}
然后,您可以在强类型视图中转换视图并在其中使用模型类
<%@ 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(Model.quadraticAValue) %>
<br />
b: <%= Html.TextBox(Model.quadraticBValue) %>
<br />
c: <%= Html.TextBox(Model.quadraticCValue) %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<p><%= Model.x1 %></p>
<p><%= Model.x2 %></p>
</div>
<% } %>
</asp:Content>
较少的魔术字符串,如果更改属性名称,则可能会出现编译时错误(如果启用了视图编译),更清晰的代码和许多其他优势等着你! :)
答案 1 :(得分:1)
在混音的某个地方你实际上需要调用QuadCalculate。我并不完全清楚你打算如何使用你在代码中维护的三对x1
和x2
。也许对三个这样的对存在的原因的一些解释可能有助于你使问题合理化。那么......为什么有三个?