如何验证从 Request.From[ " " ] 接收的数据



我正在尝试验证用户写入的数据。如果数据没有正确写入(dd/MM/yyyy),应用程序将无法工作。有什么办法吗?例如:如果日期格式正确,如果文本框为空等。这是我的视图:

@using (Html.BeginForm("About", "Home"))
{
<label for="datePicker">Type in a date:</label>
@Html.TextBox("datePicker", @DateTime.Now.ToString("dd/MM/yyyy"), new { id ="datePicker" })
<br />
<br />
<label for="datePickerStart">Type in starting date:</label>
@Html.TextBox("datePickerStart", @DateTime.Now.ToString("dd/MM/yyyy"), new { id ="datePickerStart" })
<br />
<br />
<label for="datePickerEnd">Type in ending date:</label>
@Html.TextBox("datePickerEnd", @DateTime.Now.ToString("dd/MM/yyyy"), new { id ="datePickerEnd" })
<br />
<input id="submitBtn" type="submit" value="Search" class='create__btn create__customBtn' />
<a asp-action="About">Refresh</a>
}
<p>Money earned for the selected date: @ViewBag.SelectedDateSum RON</p>
<p>Money earned in the time period selected: @ViewBag.BetweenSum RON</p>
</div>

我的控制器:

public ActionResult About(DateTime? datePicker)
{
DateTime userSelectedDate = DateTime.ParseExact(Request.Form["datePicker"].ToString(), "dd/MM/yyyy", null);

//value for a selected date
var allInvoices = _context.Invoices.Where(dd => dd.IssuedDate == userSelectedDate).ToArray();
int sumFirst = 0;
foreach (var invoice in allInvoices)
{
int x = Int32.Parse(invoice.Value);
sumFirst += x;
}
ViewBag.SelectedDateSum = sumFirst;
//value between two selected dates
DateTime startDate = DateTime.ParseExact(Request.Form["datePickerStart"].ToString(), "dd/MM/yyyy", null);
DateTime endDate = DateTime.ParseExact(Request.Form["datePickerEnd"].ToString(), "dd/MM/yyyy", null);
int sumBetween = 0;
var allInvoices1 = _context.Invoices.Where(dd => dd.IssuedDate >= startDate && dd.IssuedDate <= endDate).ToArray();
foreach (var invoice in allInvoices1)
{
int x = Int32.Parse(invoice.Value);
sumBetween += x;
}
ViewBag.BetweenSum = sumBetween;
return View();
}

我建议使用TryParseExact而不是ParseExact。您可以通过以下方式在后端捕获格式错误的用户输入:

public ActionResult About(DateTime ? datePicker) {
DateTime userSelectedDate;
//value between two selected dates
DateTime startDate;
DateTime endDate;
if (DateTime.TryParseExact(Request.Form["datePicker"].ToString(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out userSelectedDate)
&& DateTime.TryParseExact(Request.Form["datePickerStart"].ToString(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out startDate)
&& DateTime.TryParseExact(Request.Form["datePickerEnd"].ToString(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out endDate))
{
//value for a selected date
var allInvoices = _context.Invoices.Where(dd => dd.IssuedDate == userSelectedDate).ToArray();
int sumFirst = 0;
foreach (var invoice in allInvoices)
{
int x = Int32.Parse(invoice.Value);
sumFirst += x;
}
ViewBag.SelectedDateSum = sumFirst;

int sumBetween = 0;
var allInvoices1 = _context.Invoices.Where(dd => dd.IssuedDate >= startDate && dd.IssuedDate <= endDate).ToArray();
foreach (var invoice in allInvoices1)
{
int x = Int32.Parse(invoice.Value);
sumBetween += x;
}
ViewBag.BetweenSum = sumBetween;
return View();
} else
{
//Malformed date was provided

}
}

Try Parse DateTime Docs: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=net-6.0

Int32和其他数据类型也支持TryParse: https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-6.0

也有一些指南在前端提供验证,但不可否认这不是我的强项:https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/validating-user-input-in-aspnet-web-pages-sites

使用DateTime.TryParse()代替。如果返回false,则调用ModelState.AddError()并返回带有模型的视图。您可以使用验证器在视图中调用错误表单。

相关内容

最新更新