AlHal2
asked on
Event handling mvc
On my home controller I have this action result.
I have a view called maths with a button it.
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.
public ActionResult Maths()
{
ViewBag.Message = "Your contact page.";
return View();
}
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." />
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();
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
ASKER
http://localhost:52330/Home/Maths/?MaxNumberToTry=33