[英]@Html.ValidationSummary works on wrong page
Using asp.net core razor. 使用asp.net核心剃须刀。 My current if statement is wrong, but it is the only way to get the error messages to show up.
我当前的if语句是错误的,但这是使错误消息显示出来的唯一方法。 The current if statement is if the ModelState is not valid return to view.
当前的if语句是如果ModelState无效,则返回视图。 On the new view it shows the error messages.
在新视图上,它显示错误消息。 However, what I want is if the ModleState is not valid redirect to the Index.cshtml page and show the errors their.
但是,我想要的是如果ModleState无效,则重定向到Index.cshtml页面并显示错误。 When I flipped around my if condition the error messages do not show up in the Index.cshtml page.
当我翻转if条件时,错误消息不会显示在Index.cshtml页面中。
Here is my Controller. 这是我的控制器。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using quotingDojo.Models;
namespace quotingDojo.Controllers
{
public class HomeController : Controller
{
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
return View();
}
[HttpPost]
[Route("quotes")]
public IActionResult Quotes(Home model)
{
if(!ModelState.IsValid)
{
return View();
}
//continue your code to save
return RedirectToAction("Index");
}
}
}
Here is my Index.cshtml 这是我的Index.cshtml
@model quotingDojo.Models.Home
<h1>Welcome to the Quoting Dojo</h1>
@using(Html.BeginForm("Quotes","Home"))
{
<p>@Html.ValidationSummary()</p>
<p>
<label>Your Name</label>
@Html.TextBoxFor(s=>s.Name)
@Html.ValidationMessageFor(s => s.Name)
</p>
<p>
<label>Your Quote</label>
@Html.TextAreaFor(d=>d.Quote)
</p>
<input type="submit" name="submit" value="Add my quote!"/>
}
<form action="quotes" method="get">
<input type="submit" name="submit" value="Skip to quotes!"/>
</form>
Here is my Quotes.cshtml where the error messages currently show up. 这是我的Quotes.cshtml,当前显示错误消息。
<h1>Test</h1>
<p>@Html.ValidationSummary()</p>
Here is my models page using System.ComponentModel.DataAnnotations; 这是我使用System.ComponentModel.DataAnnotations的模型页面;
namespace quotingDojo.Models
{
public class Home
{
[Required(ErrorMessage ="Please enter your name")]
[DataType(DataType.Text)]
[MinLength(3)]
public string Name {get; set;}
[Required]
[MinLength(5)]
public string Quote{get; set;}
}
}
Your issue is here: 您的问题在这里:
return View();
That will return a view named "Quotes" since your action method is named Quotes
(this is MVC convention). 由于您的操作方法名为
Quotes
(这是MVC约定),因此将返回一个名为“ Quotes”的视图。 You also do not pass a model to it so even if the view existed, it will not know of the errors. 您也不会将模型传递给它,因此即使视图存在,它也不会知道错误。
Two ways to fix your issue: 解决问题的两种方法:
1. 1。
You have to pass the model to your Index
view so you need to explicitly return the Index
view. 您必须将模型传递到
Index
视图,因此您需要显式返回Index
视图。
if(!ModelState.IsValid)
{
return View("Index", model);
}
2. 2。
I personally prefer this method. 我个人更喜欢这种方法。 Name your first action which serves the original form the same as the one your are posting to and then you can do this (note: you will also need to rename the view):
命名您的第一个动作,该动作的原始形式与您要发布到的动作相同,然后可以执行此操作(注意:您还需要重命名视图):
// GET: /Home/
[HttpGet]
[Route( "" )]
public IActionResult Quotes() {
return View();
}
[HttpPost]
[Route( "quotes" )]
public IActionResult Quotes(Home model) {
if( !ModelState.IsValid ) {
return View(model);
}
//continue your code to save
return RedirectToAction( "Index" );
}
This way both of them return the view named Quotes
so you do not have to explicitly mention it. 这样,他们两个都返回了名为
Quotes
的视图,因此您不必明确提及它。
The standard practice is, If model state is not valid, you return the same view (which user submitted) where you will show the validation error messages. 标准做法是,如果模型状态无效,则返回相同的视图(用户提交的视图),其中将显示验证错误消息。 You just need to change your
Quotes
action method to Index
action method. 您只需要将
Quotes
操作方法更改为Index
操作方法即可。
[HttpPost]
[Route("quotes")]
public IActionResult Index(Home model)
{
if (!ModelState.IsValid)
return View();
// to do :continue saving
}
Make sure to update your form to post to this action. 确保更新您的表单以发布到此操作。
Not sure why you want to redirect to another page where you want to show the errros. 不确定为什么要重定向到要显示错误的其他页面。
If you absolutely want to show the error messages in another view which was loaded via RedirectToAction method call (hence a totally new GET call), You need to use TempData to transfer the errors. 如果您绝对想在通过RedirectToAction方法调用加载的另一个视图中显示错误消息(因此使用全新的GET调用),则需要使用TempData来传输错误。
public ActionResult Quotes(Home model)
{
if (!ModelState.IsValid)
{
List<string> errors = new List<string>();
foreach (var modelStateVal in ViewData.ModelState.Values)
{
foreach (var error in modelStateVal.Errors)
{
errors.Add(error.ErrorMessage);
}
}
TempData["errors"] = errors;
return RedirectToAction("Index");
}
//continue your code to save
}
And in your index view, 在您的索引视图中
@if (TempData["errors"] != null)
{
var errors = (List<string>) TempData["errors"];
foreach (var error in errors)
{
<span class="alert warning">@error</span>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.