【问题标题】:MVC View not displaying validation errorsMVC 视图不显示验证错误
【发布时间】:2014-01-17 17:57:01
【问题描述】:

我在我的 mvc 项目中得到了一些验证。但是,当我提交一个空表单或未输入某些必填字段的表单时,它不会停留在同一个表单/视图中并显示错误。我看不到任何模式错误(即金额是必填字段)

例如,这里是我的模态中的一些属性

    [Required]
    [StringLength(1, MinimumLength = 1)]
    public string Period { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public System.DateTime ModifyDate { get; set; }

这是我的控制器

[HttpPost]
         public ActionResult StopScheduled([Bind(Prefix = "item")]  BillPayModel model)
         {
             //getUsers();

             try
             {
                 if (ModelState.IsValid)
                 {

                    //save stuff into db
                     db.SaveChanges();

                 }
                 else
                 {
                     ModelState.AddModelError("", "Could not Stop Scheduled Payment");
                 }
             }


             catch (FormatException)
             {
                 ModelState.AddModelError("", "Could not Stop Scheduled Payment");
             }


             return RedirectToAction("StopScheduled");
         }

    }

这是我的看法

@if (Model !=null)
{
    if (Model.IsSuccess == true)
    {
        <span><center><font color = "green">Successfully Completed Transaction!  </font></center></span>
    }
    else
    {
         @Html.ValidationSummary(true, "ERROR! Please make sure you have entered correct details"); 
    }
}

@if (Model ==null)
{ 
    using (Html.BeginForm("BillPay", "BillPay", FormMethod.Post, new {}))
    {
        @Html.ValidationSummary(true);

        <div>@Html.LabelFor(model => model.AccountNumber)</div>
         @Html.DropDownList("Accounts",  "-- Select User --")  

        <div>@Html.LabelFor(model => model.PayeeID)</div>
        @Html.DropDownList("PayeeID",  "-- Select User --")         


        <div>@Html.LabelFor(model => model.Amount)</div>
        <div>@Html.TextBoxFor(model => model.Amount,new {style = "width:150px"})
            @Html.ValidationMessageFor(model => model.Amount)
        </div>        

        <div>@Html.LabelFor(model => model.ScheduleDate) (i.e 20/10/2013 10:00)</div>
        <div>@Html.TextBoxFor(model => model.ScheduleDate,new {style = "width:250px"})
            @Html.ValidationMessageFor(model => model.ScheduleDate)
        </div>

        <div>@Html.LabelFor(model => model.Period)</div>
        <div>@Html.TextBoxFor(model => model.Period,new {style = "width:150px"})
            @Html.ValidationMessageFor(model => model.Period)
        </div> 

        <input type="submit" value ="Submit" style="width:8%;height:5%"/> 
        <input type="reset" value ="reset" style="width:8%;height:5%"/>   
    }
}
else
{

}

【问题讨论】:

  • 您的(母版)页面中是否包含jquery.validation.js
  • 这就是我的想法:D

标签: c# asp.net-mvc validation entity-framework-4


【解决方案1】:

问题是您正在使用 RedirectToAction 发出新请求,因此您的模型验证设置为有效。要解决您的问题,您需要这样做:

[HttpPost]
public ActionResult StopScheduled([Bind(Prefix = "item")]  BillPayModel model)
{
         try
         {
             if (ModelState.IsValid)
             {
                //save stuff into db
                 db.SaveChanges();
             }
             else
             {
                 ModelState.AddModelError("", "Could not Stop Scheduled Payment");
             }
         }
         catch (FormatException)
         {
             ModelState.AddModelError("", "Could not Stop Scheduled Payment");
         }
         return View(model);
}

并且在视图中您应该将 Html.ValidationSummary excludePropertyErrors 更改为 false,如下所示:

if (Model.IsSuccess == true)
{
    <span><center><font color = "green">Successfully Completed Transaction!  </font></center></span>
}
else
{
     @Html.ValidationSummary(false, "ERROR! Please make sure you have entered correct details"); 
}

要在每个输入中显示它们,您需要更改您的 if 语句 (Model==null)。

【讨论】:

  • 这并没有真正解决它。我仍然得到的是“错误!请确保您输入了正确的详细信息”。当我提交该错误时,为什么我的 from 没有显示?
  • @kayze 是的,它只显示,因为如果模型不为空,您不会显示验证消息。
猜你喜欢
  • 1970-01-01
  • 2017-03-11
  • 2014-09-23
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2023-03-14
相关资源
最近更新 更多