我有一个简单的Web应用程序,用户在表单中输入三个整数,然后收到总和。
我的问题是:我希望在用户提交之前不会显示实际的总和值。这意味着,当页面打开时,ViewBag只显示任何内容。提交后,ViewBag内容出现。我可以使用当前的代码吗?
我知道这可能需要JavaScript功能。我只是没有将JS与C#集成的经验。
查看:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form action="" method="post">
Enter the 1st Number: <input id="firstInt" name="firstInt" type="text" value="0" /><br />
Enter the 2nd Number: <input id="secondInt" name="secondInt" type="text" value="0" /><br />
Enter the 3rd Number: <input id="thirdInt" name="thirdInt" type="text" value="0" /><br />
<input id="Submit" type="submit" value="submit" />
<input id="Reset" type="reset" value="reset" /><br /><br />
Sum = @ViewBag.result
</form>
控制器:
public class HomeController : Controller
{
public ActionResult Index(int firstInt = 0, int secondInt = 0, int thirdInt = 0)
{
int sum = firstInt + secondInt + thirdInt;
ViewBag.result = sum;
return View();
}
}
答案 0 :(得分:0)
是可能,如果您创建会话变量而不是viewBag。 您最好引用此链接How to use session variable in MVC
最佳做法是尽可能使用ajax