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



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

模型类

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

而不是

return RedirectToAction("SystemCoreHome");

return PartialView("CreateNewNavigation_Partial", obj);

阿贾克斯形式

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "TargetID" }))
{
   ...
}
<div class="_Form_Block" id="TargetID">
</div>

您需要处理这种情况。

目前,如果

模型状态无效,您没有执行任何操作。请尝试以下操作:

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

应该做这个伎俩。

相关内容

  • 没有找到相关文章

最新更新