Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Event handling mvc

On my home controller I have this action result.

        public ActionResult Maths()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

Open in new window


I have a view called maths with a button it.  

@Html.TextBox("name")
<input id="cmdCount" type="button" value="Count up to chosen number. and print to a text file." />

Open in new window


When I press the button I want it to find the prime numbers up to the number I enter in a text box using this code.
I'm not sure how to pass the value I enter in the textbox back to the controller after pressing the button.

I will want to be able to add other buttons on the same page (view) to perform other mathematical functions eg finding Pythagrean triples.  I have the code to find the triples, but don't know how to structure the view and controller.

using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintList(int.Parse(txtPrimes.Text));
        }
        public void PrintList(int MaxNumberToTry)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(@"D:\OneDrive\Documents\MyPrograms\MathsPrograms\Prime1.csv", false))
                {
                    int CheckThisNumber = 2;
                    int Counter = 2;
                    for (CheckThisNumber = 2; CheckThisNumber <= MaxNumberToTry; CheckThisNumber++)
                    {
                        Counter = 2;
                        do
                        {
                            if (CheckThisNumber % Counter == 0)
                            {
                                break;
                            }
                            else
                            {

                            }
                            Counter += 1;
                        } while (Counter * Counter < CheckThisNumber);
                        if (Counter * Counter > CheckThisNumber)
                        {
                            sw.WriteLine(CheckThisNumber);
                            Counter = 2;
                        }
                        else
                        {
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlHal2

ASKER

Thanks Chinmay.  This code has produced the file without needing to use a view.  I used this URL
http://localhost:52330/Home/Maths/?MaxNumberToTry=33
Avatar of AlHal2

ASKER

        [HttpGet]
        public ActionResult Maths(int MaxNumberToTry)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(@"D:\OneDrive\Documents\MyPrograms\MathsPrograms\Prime1.csv", false))
                {
                    int CheckThisNumber = 2;
                    int Counter = 2;
                    for (CheckThisNumber = 2; CheckThisNumber <= MaxNumberToTry; CheckThisNumber++)
                    {
                        Counter = 2;
                        do
                        {
                            if (CheckThisNumber % Counter == 0)
                            {
                                break;
                            }
                            else
                            {

                            }
                            Counter += 1;
                        } while (Counter * Counter < CheckThisNumber);
                        if (Counter * Counter > CheckThisNumber)
                        {
                            sw.WriteLine(CheckThisNumber);
                            Counter = 2;
                        }
                        else
                        {
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }

            return View();
        }

Open in new window