MVC3不断重新加载表单页面 [英] MVC3 keeps reloading form page
本文介绍了MVC3不断重新加载表单页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我是ASP.NET MVC 3的新手,我尝试将表单数据发送回服务器,但每次单击提交按钮时,表单都会重新加载,所有数据都会丢失。
以下是我的代码的一部分:
SaleController.cs
public class SaleController:Controller
{
// 获取:/ Sale /
public ViewResult MakePurchase(){
return 查看( MakePurchase);
}
[HttpPost]
public ViewResult促销(促销)
{
返回查看( 销售,销售);
}
[HttpGet]
public ViewResult Sale()
{
return View();
}
}
MakePurchase.cshtml
@ model SalesTax.Models.Sale
@ {
Layout = null;
}
< !DOCTYPE html >
< html >
< span class =code-keyword>< head >
< title > MakePurchase < / title >
< / head >
< body >
< div >
@using(Html.BeginForm())
{
< p > 产品:@ Html.TextBoxFor(x => x.Product)< / p >
< p > 数量:@ Html.TextBoxFor(x => x.Quantity)< / p >
< p > 价格:@ Html.TextBoxFor(x => x.Price)< / p >
< p > 类型:@ Html.TextBoxFor(x => x.Type)< / p >
< p > 原产地:@ Html.TextBoxFor(x => x.Origin)< / p >
< input 类型 = 提交 value = MakePurchase / >
}
< / div < span class =code-keyword>>
< / body >
< / html >
Sale.cshtml
@ model SalesTax.Models.Sale
@ {
Layout = null;
}
< !DOCTYPE html >
< html >
< span class =code-keyword>< head >
< title > Sale < / title >
< / head >
< body >
< div >
@ {double tax = Model.CalculateTax(Model.Quantity,Model.Price,Model.Type,Model.Origin); }
您对此产品的税收是:
< p > 跨度> RS。 @tax < / p > < br / >
@ {double finalPrice = Model.Price + tax;}
最终价格(含税):
< < span class =code-leadattribute> p > Rs。 @finalPrice < / p > < br / >
< / div >
< / body >
< / html >
Sale.cs
< span class =code-keyword> namespace SalesTax.Models
{
public class 促销
{
public string 产品{获得跨度>; set ; }
public int 数量{获得跨度>; set ; }
public double 价格{获得跨度>; set ; }
public string 输入{获得跨度>; set ; }
public string 来源{获得跨度>; set ; }
public double CalculateTax( int 数量, double 价格,字符串类型, string Origin){
double tax = 0 。 0 ;
for ( int i = 0 ; i < 数量; i ++)
{
if (!(Type.Equals( Book)|| Type.Equals ( 食物)|| Type.Equals( 医疗)))
{
tax + = Price * 0 。 1 ;
if (Origin.Equals( Imported ))
{
tax + = Price * 0 。 05 跨度>;
}
}
其他 {
如果( Origin.Equals( Imported))
{
tax + = Price * 0 。 05 ;
}
}
}
返回税;
}
}
}
解决方案
好像你没有任何post方法对于MakePurchase。如下所示更改MakePurchase.cshtml中的表单操作并检查
@using(Html.BeginForm(Sale,Sale,FormMethod.Post))
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
{
// 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
@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;
}
}
}
解决方案
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))
这篇关于MVC3不断重新加载表单页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文