TextBoxFor呈现为HTML,ID属性带有前缀
我有一个ASPNET MVC 2项目。 我用的时候
model.Login) %>
TexBoxFor将呈现为
模型中的字段是
[Required(ErrorMessage = "")] [DisplayName("Login")] public string Login { get; set; }
我可以使用一些前缀创建id和name属性吗? 喜欢
谢谢大家。
似乎MVC 2 RTM目前不提供此function。 您可以尝试以下扩展方法:
public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return ValidationMessageFor(htmlHelper, prefix, expression, null, new RouteValueDictionary()); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage) { return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary()); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage, object htmlAttributes) { return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage, IDictionary htmlAttributes) { return htmlHelper.ValidationMessage(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), validationMessage, htmlAttributes); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return HiddenFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return HiddenFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary htmlAttributes) { return htmlHelper.Hidden(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, htmlAttributes); /*return HiddenHelper(htmlHelper, ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, false, ExpressionHelper.GetExpressionText(expression), htmlAttributes);*/ } public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return TextAreaFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return TextAreaFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary htmlAttributes) { if (expression == null) { throw new ArgumentNullException("expression"); } string value; var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); if (modelMetadata.Model != null) value = modelMetadata.Model.ToString(); else value = String.Empty; return htmlHelper.TextArea(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), value, htmlAttributes); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return TextBoxFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return TextBoxFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary htmlAttributes) { return htmlHelper.TextBox(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, htmlAttributes); }
你总是可以设置htmlAttributes
,虽然它不是最干净的方法。
并且,您必须在所有助手中完成此操作。
<%: Html.TextBoxFor(model => model.Login, new { @id = "prefixLogin" }) %>
对于同样的问题有不同的解决方案..我已经创建了一个新的mvc测试项目,并将整个视图的web.config复制到我收到此错误的旧项目中,解决了
上述就是C#学习教程:TextBoxFor呈现为HTML,ID属性带有前缀分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1004693.html