我是MVC5的新手,我正在构建一个可以添加两个数字的简单应用程序。 的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test.Models
{
public class Solve
{
public decimal a { get; set; }
public decimal b { get; set; }
public decimal Result { get; set; }
}
}
这是我的控制器 的控制器
namespace Test.Controllers
{
public class SolveController : Controller
{
// GET: Solve
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Solve equation)
{
equation.Result = equation.a + equation.b;
ViewBag.Message = equation;
return View();
}
查看
@model Test.Models.Solve
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form method="post">
First Value: <input type="number" name="a"/><br /><br />
Second Value: <input type="number" name="b" /><br /><br />
<input type="submit" name="input" />
</form>
@{
var answer = (Solve)ViewBag.Message;
}
<p>
@answer.Result;
</p>
这是我的看法。可能问题来自这里。真的需要一些帮助。感谢
答案 0 :(得分:0)
返回您的模型,如下所示。
[HttpPost]
public ActionResult Index(Solve equation)
{
equation.Result = equation.a + equation.b;
return View(equation);
}
您的HTMl将是:
@model Test.Models.Solve
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form method="post">
First Value: <input type="number" name="a"/><br /><br />
Second Value: <input type="number" name="b" /><br /><br />
<input type="submit" name="input" />
</form>
<p>
@Model.Result;
</p>