英文:
Adding an error to ModelState and redirecting to the same page not working in ASP.NET MVC
问题
我有一个网站在本地服务器上,用户可以向我发送消息。我从表单的模态框中捕获用户的姓名、电子邮件和消息。
现在我有一个控制器,检查该电子邮件是否已存在,并获取该电子邮件地址最后一次向服务器发送消息的时间,然后计算如果时间间隔小于30分钟,就向Index页面模型添加一个模型错误,并重定向到它。
在Index页面的模型中,我检查模型状态是否有效,并显示错误消息您只允许每30分钟发送一次消息,并将该错误显示在一个span标签内,但在我的情况下ModelState.IsValid()返回true,为什么会这样,我已经进行了修改。我期望用户在尝试打开模态框后在重定向后看到以红色显示的消息。
处理请求的控制器方法如下:
public IActionResult GetAction([FromForm]string Name, [FromForm] string Email, [FromForm] string Query)
{
// 确保同一用户在发送新消息之前需要等待30分钟
// 检查该电子邮件是否已发送短信
if(_dbCtx.ClientMessages.ToList().Any(x => x.Email == Email)){
// 这意味着用户现在发送了一条消息,获取他最后一次发送的时间
var date = _dbCtx.ClientMessages.FirstOrDefault(x => x.Email == Email).Time;
var minElapsed = TimeSpan.FromMinutes(DateTime.Now.Subtract(date).TotalMinutes);
if (minElapsed.TotalMinutes < 30)
{
// 将此错误添加到Index页面模型
ModelState.AddModelError("", "您只允许每30分钟发送一条新消息");
// 重定向到Index页面
return RedirectToPage("/Index");
}
}
}
然后在index.cshtml中,我这样检查状态:
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Contact Us</h4>
@if (!ViewData.ModelState.IsValid)
{
// 在span中显示错误消息
<span id="error-message" class="text-danger">@Html.ValidationSummary(false)</span>
}
<button type="button" class="close" onclick="closeModal()" aria-hidden="true">×</button>
</div>
英文:
I have a website on a local server where users can send me a message. I capture the name, email and the message from the user from a modal on my form.
Now I have a controller that checks if that email already exists and gets the last time that email address posted a message to the server and computes if the interval is less than 30 minutes then adds a Model error to the Index Page model and redirects to it.
In the model of the Index Page, I check if the model state is valid and display the error you are only allowed to send a message every 30 minutes and display that error inside a span tag but the ModelState.IsValid() is returning true in my case, why is this when I have already modified it. Am expecting the user to be shown the message in red after user attempts to open the modal on redirect.
The controller method that handles the request is this
public IActionResult GetAction([FromForm]string Name, [FromForm] string Email, [FromForm] string Query)
{
//make sure the same user allows 30 minutes to elapse before sending a new message
//check if that email has already sent an sms
if(_dbCtx.ClientMessages.ToList().Any(x=> x.Email == Email)){
//this means the user posted a message now, get the last time he posted
var date = _dbCtx.ClientMessages.FirstOrDefault(x=> x.Email == Email).Time;
var minELAPSED = TimeSpan.FromMinutes(DateTime.Now.Subtract(date).TotalMinutes);
if (minELAPSED.TotalMinutes < 30)
{
//add this error to the index page model
ModelState.AddModelError("", "You are only allowed to send a new message once every 30 minutes");
//redirect to index
return RedirectToPage("/Index");
}
}
and then inside the index.cshtml,I check for the state like this.
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Contact Us</h4>
@if (!ViewData.ModelState.IsValid)
{
//display the error message in the span
<span id="error-message" class="text-danger" >@Html.ValidationSummary(false)</span>
}
<button type="button" class="close" onclick="closeModal()" aria-hidden="true">&times;</button>
</div>
答案1
得分: 1
因为您正在重定向,所以ModelState丢失。您可以不使用TempData吗?
public IActionResult GetAction([FromForm]string Name, [FromForm] string Email, [FromForm] string Query)
{
if(_dbCtx.ClientMessages.ToList().Any(x=> x.Email == Email)){
var date = _dbCtx.ClientMessages.FirstOrDefault(x=> x.Email == Email).Time;
var minELAPSED = TimeSpan.FromMinutes(DateTime.Now.Subtract(date).TotalMinutes);
if (minELAPSED.TotalMinutes < 30)
{
TempData["ErrorMessage"] = "您只能每30分钟发送一条新消息";
return RedirectToPage("/Index");
}
}
// 返回其他内容
}
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">联系我们</h4>
@if (TempData["ErrorMessage"] != null)
{
<span id="error-message" class="text-danger">@TempData["ErrorMessage"]</span>
}
<button type="button" class="close" onclick="closeModal()" aria-hidden="true">&times;</button>
</div>
英文:
Because you are redirecting, the ModelState is lost. Can you not use TempData?
public IActionResult GetAction([FromForm]string Name, [FromForm] string Email, [FromForm] string Query)
{
if(_dbCtx.ClientMessages.ToList().Any(x=> x.Email == Email)){
var date = _dbCtx.ClientMessages.FirstOrDefault(x=> x.Email == Email).Time;
var minELAPSED = TimeSpan.FromMinutes(DateTime.Now.Subtract(date).TotalMinutes);
if (minELAPSED.TotalMinutes < 30)
{
TempData["ErrorMessage"] = "You are only allowed to send a new message once every 30 minutes";
return RedirectToPage("/Index");
}
}
// return something else
}
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Contact Us</h4>
@if (TempData["ErrorMessage"] != null)
{
<span id="error-message" class="text-danger">@TempData["ErrorMessage"]</span>
}
<button type="button" class="close" onclick="closeModal()" aria-hidden="true">&times;</button>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论