Hi all, I am new to ASP.NET MVC 3 and I trying to send back form data to the server but every time I click on submit button the form is reloaded and all data is lost.
Below are part of my codes:
SaleController.cs
public class SaleController : Controller
{
public ViewResult MakePurchase() {
return View("MakePurchase");
}
[HttpPost]
public ViewResult Sale(Sale sale)
{
return View("Sale",sale);
}
[HttpGet]
public ViewResult Sale()
{
return View();
}
}
MakePurchase.cshtml
@model SalesTax.Models.Sale
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>MakePurchase</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<p>Product: @Html.TextBoxFor(x => x.Product)</p>
<p>Quantity: @Html.TextBoxFor(x => x.Quantity)</p>
<p>Price: @Html.TextBoxFor(x => x.Price)</p>
<p>Type: @Html.TextBoxFor(x => x.Type)</p>
<p>Origin: @Html.TextBoxFor(x => x.Origin)</p>
<input type = "submit" value="MakePurchase"/>
}
</div>
</body>
</html>
Sale.cshtml
@model SalesTax.Models.Sale
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Sale</title>
</head>
<body>
<div>
@{double tax = Model.CalculateTax(Model.Quantity, Model.Price, Model.Type, Model.Origin); }
Your tax for this Product is:
<p>Rs. @tax</p><br/>
@{double finalPrice = Model.Price + tax;}
Final Price(Tax included):
<p>Rs. @finalPrice</p><br/>
</div>
</body>
</html>
Sale.cs
namespace SalesTax.Models
{
public class Sale
{
public string Product { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
public string Type { get; set; }
public string Origin { get; set; }
public double CalculateTax(int Quantity, double Price, string Type, string Origin){
double tax = 0.0;
for (int i = 0; i < Quantity; i++)
{
if (!(Type.Equals("Book") || Type.Equals("Food") || Type.Equals("Medical")))
{
tax += Price * 0.1;
if (Origin.Equals("Imported"))
{
tax += Price * 0.05;
}
}
else{
if (Origin.Equals("Imported"))
{
tax += Price * 0.05;
}
}
}
return tax;
}
}
}