大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 如何在我自己的自定义助手中使用ASP.NET MVC ValidationMessage HTML Helper?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果用户的地址在北美,那么我希望为电话号码输入显示两个文本框,一个用于区号,另一个用于剩余的数字.如果地址在北美之外,那么我想要一个单独的文本框来显示完整的数字.
下面的代码按预期工作输出正确的文本框,但是,只要我添加了与每个文本框关联的验证,我现在从Validationmessage Helper调用的ToString()调用中抛出NullReferenceException(ValidationmessagE)帮助者@R_696_10672@!!).
public static String TelephonenumberInputListItem(this HtmlHelper Helper,String country,String northAmericanAreaCodeFormname,String northAmericanAreaCode,String northAmericanRemainingnumberFormname,String northAmericanRemainingnumber,String internationalFullnumberFormname,String internationalFullnumber) { //set up the error message and styling object errorHtmlAttributes = new { @class = "fError" }; String validationmessage = "*"; object htmlAttributes; //start building our list item tag which includes our telephone number //input and validation controls TagBuilder listItemBuilder = new TagBuilder("li"); //determine based on the country specified if this should be a North //American phone input form or an international one if (isnorthAmericanCountry(country)) { //add the text input controls htmlAttributes = new { size = 3,maxlength = 3 }; listItemBuilder.InnerHtml = Helper.TextBox(northAmericanAreaCodeFormname,northAmericanAreaCode,htmlAttributes).ToString(); htmlAttributes = new { size = 7,maxlength = 7 }; listItemBuilder.InnerHtml += Helper.TextBox(northAmericanRemainingnumberFormname,northAmericanRemainingnumber,htmlAttributes).ToString(); //Add the Validation message controls listItemBuilder.InnerHtml += Helper.Validationmessage(northAmericanAreaCodeFormname,validationmessage,errorHtmlAttributes).ToString(); listItemBuilder.InnerHtml += Helper.Validationmessage(northAmericanRemainingnumberFormname,errorHtmlAttributes).ToString(); } else { //add the text input control htmlAttributes = new { size = 15,maxlength = 15 }; listItemBuilder.InnerHtml = Helper.TextBox(internationalFullnumberFormname,internationalFullnumber,htmlAttributes).ToString(); //Add the Validation message control listItemBuilder.InnerHtml += Helper.Validationmessage(internationalFullnumberFormname,errorHtmlAttributes).ToString(); } return listItemBuilder.ToString(tagRenderMode.Normal); }
你能否帮我添加验证,以便这些输入文本框仍然是调用View中发生的整体表单验证的一部分?我应该提一下,将TextBox和Validationmessage Helper直接放在View中可以正常工作.
使用HTML Helpers有很多嗡嗡声(“如果有IF,请使用助手”任何人?),但是如果我们不能将验证控件添加到我们使用的输入控件中,我们应该如何使用它们.
预先感谢您的帮助.
由于Validationmessage助手返回一个字符串,因此没有理由调用ToString()(导致异常的原因).删除ToString,您的代码应该按预期工作.
您也可以从TextBox助手中删除ToString调用.
public static String Validationmessage(this HtmlHelper htmlHelper,String modelName,String validationmessage,IDictionary<String,object> htmlAttributes) { if (modelName == null) { throw new ArgumentNullException("modelName"); } if (!htmlHelper.ViewData.modelState.ContainsKey(modelName)) { return null; } ModelState modelState = htmlHelper.ViewData.modelState[modelName]; ModelErrorCollection modelErrors = (modelState == null) ? null : modelState.Errors; ModelError modelError = ((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors[0]; if (modelError == null) { return null; } TagBuilder builder = new TagBuilder("span"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("class",HtmlHelper.ValidationmessageCssClassName); builder.SeTinnerText(String.IsNullOrEmpty(validationmessagE) ? GetUserErrormessageOrDefault(htmlHelper.ViewContext.httpContext,modelError,modelStatE) : validationmessagE); return builder.ToString(tagRenderMode.Normal); }
以上是大佬教程为你收集整理的asp.net-mvc – 如何在我自己的自定义助手中使用ASP.NET MVC ValidationMessage HTML Helper?全部内容,希望文章能够帮你解决asp.net-mvc – 如何在我自己的自定义助手中使用ASP.NET MVC ValidationMessage HTML Helper?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。