我正在嘗試驗證用戶寫入的資料。如果資料未正確寫入 (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();
}
uj5u.com熱心網友回復:
我建議使用 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
}
}
嘗試決議 DateTime 檔案:https ://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=net-6.0
Int32 和其他資料型別也支持 TryParse:https ://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-6.0
還有一些指南可以在前端提供驗證,但不可否認,這不是我的強項: https ://docs.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes /validating-user-input-in-aspnet-web-pages-sites
uj5u.com熱心網友回復:
改為使用DateTime.TryParse()。如果它回傳 false,則呼叫ModelState.AddError()并回傳帶有模型的視圖。您可以使用驗證器在視圖中調出錯誤表單。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483859.html
標籤:C# asp.net-mvc
上一篇:無法列出所有角色
