Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
public class SaleController : Controller
    {
        // GET: /Sale/
        public ViewResult MakePurchase() {
            return View("MakePurchase");
        }

        [HttpPost]
        public ViewResult Sale(Sale sale)
        {
                return View("Sale",sale);
            
        }

        [HttpGet]
        public ViewResult Sale()
        {
            return View();
        }

    }


MakePurchase.cshtml
HTML
@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
HTML
@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
C#
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;
        }

    }
}
Posted
Updated 17-Aug-15 22:11pm
v4

1 solution

seems you dont have any post method for MakePurchase. Change the form action in MakePurchase.cshtml as below and check

@using (Html.BeginForm("Sale","Sale", FormMethod.Post))
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900