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