【发布时间】:2017-10-12 13:13:39
【问题描述】:
我对在 C# 中使用 Razor 非常陌生,并且正在开展这个项目以试图更好地理解它。
这个应用程序应该在完成后要求用户输入三个整数,然后打印出这些整数的总和。现在,我已经设置了视图和控制器的基本框架。 (目前没有模型。)控制器设置为使用 HTTP-Post 协议,以便将信息发送到 HTML 表单。
我正在努力解决的是将数据直接传递给表单所需的代码,以及所需的任何参数,以便 ASP.net 将忽略两个同名控制器操作的存在(我m 告诉它应该能够在正确设置 Razor 语法后执行此操作)。
这里的任何指导都会非常有帮助。 (注意:我在完成的解决方案中要求使用 HTTP Post 协议。)
控制器:
public ActionResult Index(int firstInt = 0, int secondInt = 0, int thirdInt = 0)
{
return View();
}
[HttpPost]
public ActionResult Index(int firstInt = 0, int secondInt = 0, int thirdInt = 0)
{
int sum = firstInt + secondInt + thirdInt;
ViewBag.result = sum;
}
索引视图:
<form action="" method="post">
<table>
<tr><td>Enter the 1st Number: <input id="firstInt" name="firstInt" type="text" value="0" /></td></tr>
<tr><td>Enter the 2nd Number: <input id="secondInt" name="secondInt" type="text" value="0" /></td></tr>
<tr><td>Enter the 3rd Number: <input id="thirdInt" name="thirdInt" type="text" value="0" /></td></tr>
<tr>
<td><input id="Submit" type="submit" value="submit" /><input id="Reset" type="reset" value="reset" /></td>
</tr>
<tr>
<td>Sum = @ViewBag.result</td>
</tr>
</table>
</form>
【问题讨论】:
标签: c# asp.net asp.net-mvc razor http-post