我想扩展帮助程序,使它像这样:
@html.TextBoxFor(x=>x.CustomerId).ReadOnly()
并输出没有name属性的input元素,这样它就不会发布到服务器上.
解决方法
这应该是诀窍:
public static class MyInputExtensions { public static MvcHtmlString NameLessTextBoxFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression) { var textBox = htmlHelper.TextBoxFor(expression); string pattern = @"name=""([^""]*)"""; string fixedHtml = Regex.Replace(textBox.ToHtmlString(),pattern,""); return new MvcHtmlString(fixedHtml); } }
用法:
@Html.NameLessTextBoxFor(x=> x.CustomerId)