我有一个非常基本的MVC项目,可以完成2个数字的数学运算(+ - / *)
由于某些原因,我的观点在回发后没有更新
这是我的控制器
namespace MathsAppMVC.Controllers
{
public class HomeController : Controller
{
MathsServiceClient loClient = new MathsServiceClient();
Int32 loNum1 = 0;
Int32 loNum2 = 0;
//Int32 result = 0;
String locOperation = "Add";
public ActionResult Index()
{
var model = new MathsModel
{
Number1 = loNum1,
Number2 = loNum2,
//Result = result,
MathsOperation = locOperation
};
return View(model);
}
[HttpGet]
public ActionResult MathsOperation()
{
return View();
}
[HttpPost]
public ActionResult MathsOperation(MathsModel mathsModel)
{
loNum1 = mathsModel.Number1;
loNum2 = mathsModel.Number2;
locOperation = mathsModel.MathsOperation;
if (locOperation == "Add")
{
mathsModel.Result = loClient.add(loNum1, loNum2);
}
else if (locOperation == "Subtract")
{
mathsModel.Result = loClient.subtract(loNum1, loNum2);
}
else if (locOperation == "Multiple")
{
mathsModel.Result = loClient.multiple(loNum1, loNum2);
}
else
if (locOperation == "Divide")
{
mathsModel.Result = loClient.divide(loNum1, loNum2);
}
if (ModelState.IsValid)
{
return View("Index", mathsModel);
}
else
{
return View("Index");
}
}
}
}
这是观点
@model MathsAppMVC.Models.MathsModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("MathsOperation", "Home", FormMethod.Post, new { }))
{
<fieldset>
<legend>Maths:</legend>
<div>@Html.LabelFor(u=>u.MathsOperation)</div>
<div>@Html.DropDownListFor(u => u.MathsOperation, new SelectList(
new List<Object>
{
new { value = "Add" , text = "Add" },
new { value = "Subtract" , text = "Subtract" },
new { value = "Multiple" , text = "Multiple"},
new { value = "Divide" , text = "Divide"}
},
"value",
"text",
0))
</div>
<div>@Html.LabelFor(u=>u.Number1)</div>
<div>@Html.TextBoxFor(u=>u.Number1)</div>
<div>@Html.LabelFor(u=>u.Number2)</div>
<div>@Html.TextBoxFor(u=>u.Number2)</div>
<div>@Html.LabelFor(u=>u.Result)</div>
<div>@Html.DisplayTextFor(u=>u.Result)</div>
<input type="submit" value ="Calculate" />
<input type="reset" value ="Clear" />
</fieldset>
}
在回发后的视图中,结果始终为0。
有人请帮忙吗?
答案 0 :(得分:0)
我讨厌发布这个答案,但这就是我能用我的声誉所做的一切。我把你的代码复制并粘贴到一个新的MVC项目中,它运行正常。你可以发布你的MathsServiceClient代码吗?