我正在寻找一种方法来验证ASP View页面上的两个字段.我知道验证表单字段的常用方法是在页面上包含一些@model viewmodel对象,其中此模型对象的属性将使用验证所需的适当注释进行注释.例如,注释可以是这样的:
[required(ErrorMessage = "Please add the message")] [Display(Name = "Message")]
但是,在我的情况下,页面上没有包含模型,并且从表单调用的控制器操作接收平面字符串作为方法参数.
这是表单代码:
@using (Html.BeginForm("InsertRSSFeed","RSS",FormMethod.Post,new { @id = "insertForm",@name = "insertForm" })) { <!-- inside this div goes entire form for adding RSSFeed,or for editing it --> ... <div class="form-group"> <label class="col-sm-2 control-label"> Name:</label> <div class="col-sm-10"> <div class="controls"> @Html.Editor("Name",new { htmlAttributes = new { @class = "form-control",@id = "add_RSSFeed_name" } }) </div> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label"> URL:</label> <div class="col-sm-10"> <div class="controls"> @Html.Editor("Url",@id = "add_RSSFeed_Url" } }) </div> </div> </div> </div> </div> <!-- ok and cancel buttons. they use two css classes. button-styleCancel is grey button and button-styleOK is normal orange button --> <div class="modal-footer"> <button type="button" class="button-styleCancel" data-dismiss="modal">Close</button> <button type="submit" class="button-styleOK" id="submitRSSFeed">Save RSS Feed</button> </div> }
您可以看到该表单正在向RSSController操作方法发送两个文本字段(Name和Url),该方法接受这两个字符串参数:
[HttpPost] public ActionResult InsertRSSFeed(string Name,string Url) { if (!String.IsNullOrEmpty(Name.Trim()) & !String.IsNullOrEmpty(Url.Trim())) { var RSSFeed = new RSSFeed(); RSSFeed.Name = Name; RSSFeed.Url = Url; using (AuthenticationManager authenticationManager = new AuthenticationManager(User)) { string userSid = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.PrimarySid); string userUPN = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.Upn); RSSFeedService.CreateRSSFeed(RSSFeed); } } return RedirectToAction("ReadAllRSSFeeds","RSS"); }
如果页面有模型,验证将使用@ Html.ValidationSummary方法完成,但正如我所说,我没有在页面上使用modelview对象.
有没有办法在不使用ModelView对象的情况下实现这种验证,以及如何做到这一点?谢谢.
解决方法
如果您正在寻找服务器端验证,您可以使用以下内容
ModelState.AddModelError("","Name and Url are required fields.");
但你需要添加
@Html.ValidationSummary(false)
在Html.BeginForm部分内的剃刀视图中,代码将如下所示.
[HttpPost] public ActionResult InsertRSSFeed(string Name,string Url) { if (String.IsNullOrEmpty(Name.Trim()) || String.IsNullOrEmpty(Url.Trim())) { ModelState.AddModelError("","Name and URL are required fields."); return View(); } var RSSFeed = new RSSFeed(); RSSFeed.Name = Name; RSSFeed.Url = Url; using (AuthenticationManager authenticationManager = new AuthenticationManager(User)) { string userSid = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.PrimarySid); string userUPN = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.Upn); RSSFeedService.CreateRSSFeed(RSSFeed); } return RedirectToAction("ReadAllRSSFeeds","RSS"); }
如果您只查找客户端验证,那么您必须使用Jquery等客户端验证库.
http://runnable.com/UZJ24Io3XEw2AABU/how-to-validate-forms-in-jquery-for-validation
编辑部分征求意见
你的剃须刀应该是这样的.
@using (Html.BeginForm("InsertRSSFeed",@name = "insertForm" })) { @Html.ValidationSummary(false) <div class="form-group"> <label class="col-sm-2 control-label"> Name:</label> <div class="col-sm-10"> <div class="controls"> @Html.Editor("Name",@id = "add_RSSFeed_name" } }) </div> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label"> URL:</label> <div class="col-sm-10"> <div class="controls"> @Html.Editor("Url",@id = "add_RSSFeed_Url" } }) </div> </div> </div> </div> </div> <!-- ok and cancel buttons. they use two css classes. button-styleCancel is grey button and button-styleOK is normal orange button --> <div class="modal-footer"> <button type="button" class="button-styleCancel" data-dismiss="modal">Close</button> <button type="submit" class="button-styleOK" id="submitRSSFeed">Save RSS Feed</button> </div> }
希望这可以帮助.