gpt4 book ai didi

javascript - asp.net 验证用户输入但保留字段

转载 作者:行者123 更新时间:2023-12-03 08:52:48 26 4
gpt4 key购买 nike

我正在开发一个时间报告系统,只是为了更好地掌握 asp.net mvc5 和 javascript 等。这是我的观点: enter image description here

如果 Projekt 1 上的 Timmar: 与“Tid Jobbad:”不匹配,它将被发送到后端并检查时间是否正确。如果它们不相加,我会返回如下错误消息:

ModelState.AddModelError("hours_" + project.First(), "Antalet timmar stämmer ej överrens.");

但这也意味着我的 View 被重置为原始状态,但我想做的就是保留我输入的所有字段并仍然收到错误消息。

我想我可以编写一个 javascript 来执行此检查,但是让 Javascript 执行这样的检查是一个好主意吗,因为您可以很容易地篡改它?

这是我的 Controller 以及我当前验证时间的方式。

private NewTimeReportModel ReportDates(FormCollection form, NewTimeReportModel projectData, List<string> dates, bool? noTimeToReport)
{
foreach (string date in dates)
{
DateTime reportDate;
bool? noTime = noTimeToReport;

if (!DateTime.TryParse(date, out reportDate))
{
ModelState.AddModelError("Date", "Inget datum valt");
}
else
{
var projectNumbers = (from x in form.AllKeys where x.Contains("_") select x.Substring(x.IndexOf('_'))).Distinct();

projectData.Times = new TimeReportTimes(form["startTime"], form["endTime"], form["breakTime"], ModelState);
projectData.Date = reportDate;

//Will never be null.
if (noTime != null)
{
projectData.NoTimeToReport = (bool)noTime;

if (!(bool)noTime)
{
//Validates so the user put in the right amount of hours, and did chosoe a project.
ValidateUserInput(form, projectData, projectNumbers);

if (!string.IsNullOrEmpty(form["ReportId"]))
{
projectData.ReportId = Guid.Parse(form["ReportId"]);
}
}
}

if (ModelState.IsValid)
{
//If we get this far everything is ok and we save the timereport to the database
projectData.SaveToDatabase(Constants.CurrentUser(User.Identity.Name));
ViewData["posted"] = true;
projectData = new NewTimeReportModel();
}
else if (projectData.Projects.Count == 1)
{
projectData.Projects[0].Hours = null;
}
}
}

return projectData;
}
private void ValidateUserInput(FormCollection form, NewTimeReportModel projectData, IEnumerable<string> projectNumbers)
{
IEnumerable<string> project = projectNumbers as string[] ?? projectNumbers.ToArray();
if (!project.Any())
{
ModelState.AddModelError("Projekt", "Inga projekt valda...");
}
else
{
int emptyHours = 0;
foreach (string projectNumber in project)
{
projectData.Projects.Add(new NewTimeReportModel.Project
{
Description = form["description" + projectNumber],
Hours = null,
ProjectId = Guid.Parse(form["project" + projectNumber])
});

string hourString = form["hours" + projectNumber];
if (string.IsNullOrEmpty(hourString))
{
emptyHours++;
int count = project.Count();
if (count > 1)
{
ModelState.AddModelError("hours_" + projectNumber, "Du måste skriva in tid på alla project.");
}
projectData.Projects[projectData.Projects.Count - 1].Hours = projectData.Times.WorkedHours;
}
else
{
if (!projectData.Projects[projectData.Projects.Count - 1].SetHours(hourString))
{
ModelState.AddModelError("hours_" + projectNumber, "Felaktig antal timmar.");
}
}
}

if (emptyHours > 1 || (emptyHours == 0 && projectData.Projects.Sum(x => x.Hours) != projectData.Times.WorkedHours))
{
ModelState.AddModelError("hours_" + project.First(), "Antalet timmar stämmer ej överrens.");
}

if (projectData.Projects.Any(x => x.Hours <= 0))
{
ModelState.AddModelError("hours_" + project.First(), "Antalet timmar jobbade måste vara större än noll.");
}
}
}

最佳答案

不是只返回 View ,而是返回带有模型的 View ,如下所示返回 View (模型);

检查以下链接以获取代码 http://www.codeproject.com/Articles/710702/ASP-NET-MVC-Server-Side-Validation

关于javascript - asp.net 验证用户输入但保留字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32624167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com