views:

52

answers:

2

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
            {
                //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
+1  A: 

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?

spender
+4  A: 

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! :)

Lorenzo
cool profile pic
CRice
Thanks a lot for these detailed responses :) I'm trying to wrap my head around this, and it seems to produce really clean and separated code.
BOSS
One thing though, how can I use the instance variables from one class in another class. How do I make a reference to that class in a different class?
BOSS
@BOSS: on the asp.net web site `http://www.asp.net/mvc` you can can find a great series of tutorials to open your mind on the general idea. My advice is to spend some time looking at them. You'll love it...
Lorenzo
@CRice: Thanks! That's after a deep debugging session :)
Lorenzo
@BOSS: If I unedrstand your meaning.... You can have complex model class that group simple model classes. Dont think at model classes as only object that describes your Entities. You can have as many View Model classes as you want sometimes even to just support a complex view.
Lorenzo
Yes, just map your actual model to these view model classes so that they are perfect for what you need. Automapper can make this step automated too. You can also add things like validation attributes on the view model classes.
CRice
Thanks for the help guys. Sorry for all the neewby questions, but when your referring `Model` in the view page, is the specific name of your Model class containing those instance variables for the calculation? Or is it just general?
BOSS
It's the Model property of the ViewPage class that represent your View. When you create a strong-typed view that property contains an instance of the model class you have binded to it in the controller. Please see `http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.model(v=VS.90).aspx` for further details.
Lorenzo