VerfyCodeExtensions.cs 1.4 KB

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public static class VerfyCodeExtensions
  10. {
  11. public static MvcHtmlString VerfyCodeFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string url)
  12. {
  13. if (expression == null)
  14. {
  15. throw new ArgumentNullException("expression");
  16. }
  17. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  18. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  19. StringBuilder sb = new StringBuilder();
  20. sb.AppendFormat("<input type=\"text\" size=\"8\" name=\"{0}\" ", fullHtmlFieldName);
  21. sb.Append(" style=\"float: left; margin-right: 4px;border: 1px solid #cbcbcb;\" tabIndex=\"0\" placeholder=\"请输入右侧验证码\" />");
  22. sb.AppendFormat("<img style=\"cursor: hand;\" id=\"vcodeimg\" title=\"点击刷新验证码\" onclick=\"this.src='{0}?' + Math.random()\"", url);
  23. sb.AppendFormat(" src=\"{0}\" alt=\"\" />", url);
  24. return MvcHtmlString.Create(sb.ToString());
  25. }
  26. }
  27. }