我有这个问题,以确定用户的输入是否是闰年。不过,我不知道如何验证输入只是正整数。我需要在RAZOR语法中排除float类型。请问有人能帮我解决这些问题吗?
@{
ViewBag.Title = "Index";
}
@{
var leap_year = "";
var error_mes = "";
if (IsPost)
{
var yr = Request["year"];
if (yr.AsInt() < 0)
{
error_mes = "Please Input positive integer";
}
else if ((yr.AsInt()%4 == 0) || (yr.AsInt()%400 == 0))
{
leap_year = yr + " is leap year";
}
else
{
leap_year = yr + " is not leap year";
}
}
}
<html>
<head>
<title>Leap Year</title>
</head>
<body>
<h3>Please input to check for leap year</h3>
<form method="post">
<label for="year">Year</label><br />
<p><input type="text" name="year" /></p>
<p><input type="submit" value="Check" /></p>
</form>
<p>@error_mes @leap_year</p>
</body>
</html>
ASP.NET Web页面框架提供了一个IsInt()
帮助器方法,用于确定值是否为整数:
if(Request["year"].IsInt())
{
if(Request["year"].AsInt() < 0)
{
...blah blah blah
}
}
else
{
error_mes = "Please Input integer";
}
该方法是Int32.TryParse方法的包装器。