gpt4 book ai didi

c# - 在 ASP.NET MVC5 中求解二次方程

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:41 24 4
gpt4 key购买 nike

我正在尝试构建我的第一个求解二次方程的 ASP.NET MVC 应用程序,但我在将结果显示到 View 时遇到问题。这是我的模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Resolve.Models
{
public class Solve
{
public double a { get; set; }
public double b { get; set; }
public double c { get; set; }
public double x1 { get; set; }
public double x2 { get; set; }

public void Calculate (out double x1, out double x2)
{
double inside = (b * b) - 4 * a * c;

if (inside < 0)
{
x1 = double.NaN;
x2 = double.NaN;
}
else
{
double eqn = Math.Sqrt(inside);
x1 = (-b + eqn) / (2 * a);
x2 = (-b - eqn) / (2 * a);
}
}
}
}

Controller

namespace Resolve.Controllers
{
public class EquationController : Controller
{
// GET: Equation
public ActionResult Promise()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Promise(Solve equation)
{

return View(equation);
}
}
}

查看

  @model Resolve.Models.Solve

@{
ViewBag.Title = "Promise";
}

<h2>Promise</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Solve</h4>
<hr />

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.a, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.a, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.a, "", new { @class =
"text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.b, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.b, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.b, "", new { @class =
"text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.c, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.c, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.c, "", new { @class =
"text-danger" })
</div>
</div>

<p>Model.x1 : </p> <p>Model.x2</p>



<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Solve" class="btn btn-default" />
</div>
</div>
</div>
}



@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

我在向 View 显示结果时遇到了问题

最佳答案

这里有几个问题。

首先,您从一开始就不会调用Calculate 函数。因此,您的 Solve 模型从未计算过它们的 x1x2

其次,由于您将两个参数 x1x2 作为 Calculate 函数的输入,它们“屏蔽”了 Solve 模型字段 x1x2 - 用于在 View 中显示结果。你也应该把它们拿出来。

第三,正如您所发现的,Model.x1Model.x2 的语法应该使用 @ 作为前缀(即 @Model.x1) 表明它是一个 Razor 语法。

这是你应该做的:

模型方法

public void Calculate () //removes the out double for x1 and x2
{
double inside = (b * b) - 4 * a * c;

if (inside < 0)
{
x1 = double.NaN;
x2 = double.NaN;
}
else
{
double eqn = Math.Sqrt(inside);
x1 = (-b + eqn) / (2 * a);
x2 = (-b - eqn) / (2 * a);
}
}

Controller

namespace Resolve.Controllers
{

public class EquationController : Controller
{
// GET: Equation
public ActionResult Promise()
{
return View();
}

//[AcceptVerbs(HttpVerbs.Post)] //don't use this
[HttpPost] //use this instead
public ActionResult Promise(Solve equation)
{
equation.Calculate(); //calls this
return View(equation);
}
}

}

查看

<p>@Model.x1 : </p> <p>@Model.x2</p>

关于c# - 在 ASP.NET MVC5 中求解二次方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com