我正在尝试构建我的第一个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);
        }
    }
}
}


控制者

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


我确实无法在视图中显示结果

最佳答案

这里有两个问题。

首先,您永远不会首先调用Calculate函数。因此,您的Solve模型永远不会计算出它们的x1x2

其次,由于您将两个参数x1x2用作Calculate函数的输入,因此它们“屏蔽”了Solve模型字段x1x2-您可使用它们在视图中显示结果。 。您还应该将它们取出。

第三,正如您所发现的,Model.x1Model.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);
        }
     }

 }


视图

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

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

10-13 06:20