ASP.NET MVC-5 服务器端验证不工作

ASP.NET MVC-5 sever side validation not working

我有 ASP.NET MVC 应用程序,但无法对部分页面进行服务器端和客户端验证。在我的应用程序的开头,有一个可以正确验证的登录页面。所以如果我在表单中没有任何值的情况下按下提交,应用程序不会显示任何错误消息

型号class

 public class CreateFunctionNavigation_SP_Map
{
    public CreateFunctionNavigation_SP_Map()
    {
    }

    //Function Table

    [StringLength(250)]
    [Required(ErrorMessage = "Required Function Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Function Hierarchy; i.e Where Function Exists In Hierarchy Tree \n Top-Level Start From 1 ")]
    [Display(Name = "Function Hierarchy Level")]
    public int FunctionHierarchy_Level { get; set; }

    //Controller Table

    [StringLength(250)]
    [Required(ErrorMessage = "Required Controller Title")]
    [Display(Name = "Controller Title")]
    public string ControllerName { get; set; }

    //Action Table

    [StringLength(250)]
    [Required(ErrorMessage = "Required Action Title")]
    [Display(Name = "Action Title")]
    public string ActionName { get; set; }

    // Hierarchy Table

    [Required(ErrorMessage = "Required Function Parent - Child Relation ID \n Put 0 In Case Given Function doesn't Have Any Parent Function ")]
    [Display(Name = "Function Parent's FunctionID")]
    public int Function_ParentsFunctionID { get; set; }     
}

控制器方法

#region CreateNewFunctionNavigation
    [HttpGet]
    public ActionResult CreateNewFunctionNavigation()
    {

        return PartialView("CreateNewNavigation_Partial");
    }
    #endregion

    [HttpPost]
    public ActionResult CreateNewFunctionNavigation(CreateFunctionNavigation_SP_Map obj )
    {            
        try
        {
            if(ModelState.IsValid)
            {
                _FN_Services_a2.CreateFunctionNavigation(obj);
            }            
        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Unable To Create New Function Navigation" + ex);
        }

        return RedirectToAction("SystemCoreHome");
    } //end

查看

@model App.DAL.Model.CreateFunctionNavigation_SP_Map
 <div class="_Form_Block">

        @using (Html.BeginForm("CreateNewFunctionNavigation", "SystemCore", FormMethod.Post, new { id = "NewFunctionNavigationForm" }))
        {
            @Html.AntiForgeryToken()

            @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.LabelFor(@model => @model.FunctionName, new { @class = "control-label col-md-2" })
                <div class="form-group">
                    @Html.EditorFor(@model => @model.FunctionName)
                    @Html.ValidationMessageFor(@model => @model.FunctionName)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(@model => @model.FunctionHierarchy_Level, new { @class = "control-label col-md-2" })
                <div class="form-group">
                    @Html.EditorFor(@model => @model.FunctionHierarchy_Level)
                    @Html.ValidationMessageFor(@model => @model.FunctionHierarchy_Level)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(@model => @model.ControllerName, new { @class = "control-label col-md-2" })
                <div class="form-group">
                    @Html.EditorFor(@model => @model.ControllerName)
                    @Html.ValidationMessageFor(@model => @model.ControllerName)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(@model => @model.ActionName, new { @class = "control-label col-md-2" })
                <div class="form-group">
                    @Html.EditorFor(@model => @model.ActionName)
                    @Html.ValidationMessageFor(@model => @model.ActionName)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(@model => @model.Function_ParentsFunctionID, new { @class = "control-label col-md-2" })
                <div class="form-group">
                    @Html.EditorFor(@model => @model.Function_ParentsFunctionID)
                    @Html.ValidationMessageFor(@model => @model.Function_ParentsFunctionID)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default _formButton" />
                    <input type="button" value="Cancel" class="btn btn-default _formButton" onclick="CancelPage();" />
                </div>
            </div>
        }
    </div> <!--End _Form_Block-->

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

你需要处理这个案子。

目前您没有在 ModelState 无效的情况下做任何事情。请尝试以下操作:

public ActionResult CreateNewFunctionNavigation(CreateFunctionNavigation_SP_Map obj)
{
  if (!ModelState.IsValid) 
  {
    return View(obj);
  }
// rest of method here

而不是

return RedirectToAction("SystemCoreHome");

使用

return PartialView("CreateNewNavigation_Partial", obj);

Ajax表格

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "TargetID" }))
{
   ...
}

<div class="_Form_Block" id="TargetID">
</div>
if(ModelState.IsValid)
{
    _FN_Services_a2.CreateFunctionNavigation(obj);
} 
else 
{
    return View();
} 

应该可以解决问题。