asp.net-mvc – 如何在我自己的自定义助手中使用ASP.NET MVC ValidationMessage H
我正在尝试创建一个自定义的 HTML Helper,它封装了一些表示逻辑,因为我必须在同一页面上以及将来重复使用这个逻辑几次. 如果用户的地址在北美,那么我希望为电话号码输入显示两个文本框,一个用于区号,另一个用于剩余的数字.如果地址在北美之外,那么我想要一个单独的文本框来显示完整的数字. 下面的代码按预期工作输出正确的文本框,但是,只要我添加了与每个文本框关联的验证,我现在从ValidationMessage Helper调用的ToString()调用中抛出NullReferenceException(ValidationMessage)帮助者返回空值!!). 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帮助程序返回null.看下面的实际代码……由于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); } (编辑:百色站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .net – asp:GridView文本框始终返回空值
- asp.net – 允许服务器/ usercontrol上的任何属性/属性
- 如何刷新#include文件 – 它们在IIS7 / ASP.NET上进行缓存
- asp.net-mvc – ASP.NET MVC项目架构
- asp.net – $(“#dialog”).parent().appendTo($(“form:f
- asp.net读取excel文件的三种方法示例
- asp.net web.config加密解密方法
- asp.net – 登录后对Membership.GetAllUsers()的例外情况:
- 在asp.net App_Code目录中使用Nemerle
- 如何配置asp.net进程在域帐户下运行?
- asp.net – 如何查看Chrome开发者工具中发布到表
- .net – 如何正确处理n层应用程序中的错误?
- asp.net – 使用JavaScript重新排列的ListBox元素
- asp.net – IControllerFactory’MyWebSite.WebU
- asp.net-mvc-3 – ASP.Net MVC 3:在哪里处理会话
- Asp.net超链接控件相当于
- asp.net – HttpWebRequest正在为404抛出异常
- 详解ASP.NET Core 中的框架级依赖注入
- asp.net-core – 编译netcoreapp1.0,代码包含#if
- asp.net-mvc – 从ASP.NET Web API返回一些字段