2016-12-30 54 views
0

使用asp.net核心剃须刀。我目前的if语句是错误的,但这是获取错误消息的唯一方法。当前if语句是如果ModelState无效返回查看。在新视图上显示错误消息。但是,我想要的是,如果ModleState无效,重定向到Index.cshtml页面并显示错误。当我翻转if条件时,错误消息不会显示在Index.cshtml页面中。@ Html.ValidationSummary在错误页面上工作

这是我的控制器。

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"); 
     }  
    } 
} 

这里是我的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> 

这里是我的Quotes.cshtml其中的错误消息目前显示出来。

<h1>Test</h1> 
<p>@Html.ValidationSummary()</p> 

下面是使用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;} 
    } 
} 

回答

1

你的问题就在这里:

return View(); 

这将返回因为你的操作方法被命名为Quotes名为“行情”的看法(这是MVC约定)。您也不会将模型传递给它,因此即使视图存在,它也不会知道错误。

两种方式来解决您的问题:

你必须对模型传递到您的Index视图,所以你需要明确地返回Index视图。

if(!ModelState.IsValid) 
{ 
    return View("Index", model); 
} 

2.

我个人比较喜欢这种方法。命名服务于原始形式一样的一个你张贴到你的第一个动作,然后你可以这样做(注意:您还需要重命名视图):

// 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"); 
} 

这样两者的回报名为Quotes的视图,因此您不必明确提及它。

0

标准做法是,如果模型状态无效,则返回相同视图(哪个用户提交),您将在其中显示验证错误消息。您只需要将您的Quotes操作方法更改为Index操作方法。

[HttpPost] 
[Route("quotes")] 
public IActionResult Index(Home model) 
{ 
    if (!ModelState.IsValid) 
     return View(); 
    // to do :continue saving 
} 

请务必更新您的表单以发布到此操作。

不确定为什么你要重定向到你想显示errros的另一个页面。

如果您绝对想在通过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 

} 

而在你的索引视图,

@if (TempData["errors"] != null) 
{ 
    var errors = (List<string>) TempData["errors"]; 
    foreach (var error in errors) 
    { 
     <span class="alert warning">@error</span> 
    } 
} 
+0

他并不需要做到这一点。 MVC将从模型状态中选取错误。 – CodingYoshi

+0

否。如果您要将RedrectResult返回给索引,您的浏览器正在对Index进行全新的HTTP GET调用。所以你需要使用TempData来传输错误。模型状态字典将是空的(HTTP是无状态的,它不知道以前的调用是什么) – Shyju

+0

但他只会在保存完所有内容后才会这样做,然后他只想回到索引视图。换句话说,他有这样的评论:“//继续你的代码保存”,这意味着他将进行保存然后重定向。 – CodingYoshi

相关问题