开发者

ASP.NET MVC Application not calculating, n00b question

OK, I have a problem where my calculation never takes place, like it is supposed to. i'm trying to make a computation which solves for the roots of a quadratic equation using the quadratic formula, based on the values of a, b and c entered in a primitive form.

Now, let me supply the code for my Model, view and Controller for this application:

Model:

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
            {
                /开发者_Go百科/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);
            }
        }
    }
}

View:

<%@ 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>

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

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

Now, I'm trying to still work this out, but I believe the problem may lie in the assigning of the inputted values from the form, into a new object for computation. This could also be a problem considering that MVC will probably take in the form input a string stype, instead of a double, where in my model, I perform computations based off of the input being a double type.

I do not know where to make the string to double conversion if this even is necessary however.

Does anyone see why there is a problem.

The page outputs the following:

x1 = 0
x2 = 0


You never call the QuadCalculate method.

Beside of this I would suggest to move out the QuadCalculate from the view model. This latest advice does not affect in any way the actual answer. It's just a respectful way of implementing the MVC pattern.

For example:

Your model class

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

and your controller methods

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

You can then transform your view in a strong-typed view and use your model class inside it

<%@ 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>

Less magic strings, if you change a property name you can get a compile time error (if you enable view compilation), cleaner code and many other advantage are just waiting for you! :)


Somewhere in the mix you actually need to call QuadCalculate. I'm not exactly clear about how you intend to use the three different pairs of x1 and x2 that you maintain in your code. Perhaps some explanation of why three such pairs exist might help you rationalise the problem. So... why are there three?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜