解决ASP.NET MVC5中的二次方程

时间:2017-07-18 02:32:57

标签: c# asp.net asp.net-mvc-5

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

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")
}

查看

String[] letters = new String[26];
for (int i = 0; i < 26; i++) {
    letters[i] =  (char)('a' + i) + "";
}
System.out.println(Arrays.toString(letters));

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

1 个答案:

答案 0 :(得分:1)

这里有几个问题。

首先,您从不首先调用Calculate函数。因此,您的Solve模型从未计算出x1x2

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

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

这是你应该做的:

模型方法

@Model.x1

<强>控制器

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);
    }
}

查看

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);
        }
     }         

 }