在 asp net mvc 中制作自定义助手的更好方法
我只是在 asp net mvc 3 中编写了一个自定义 mvc 帮助程序,它可以工作..但它看起来不太好:
public static MvcHtmlString BsCheckBox<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
string fieldName = ExpressionHelper.GetExpressionText(expression);
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return MvcHtmlString.Create(
"<input type=\"checkbox\" name=\"" + fieldName+ "_cb\" " + (metadata.Model.ToString() == "S"
? "checked='checked'"
: "") + "/>" +
"<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + metadata.Model.ToString() + "\" />"
);
}
该帮助程序创建两个输入标记元素来控制复选框。 “S”==真 "N" == false
有更好的方法来写同样的东西吗?
编辑:
TagBuilder 看起来有点冗长:
string campo = ExpressionHelper.GetExpressionText(expression);
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
TagBuilder element1 = new TagBuilder("input");
element1.MergeAttribute("type","checkbox");
element1.MergeAttribute("name", campo + "_cb");
if (metadata.Model.ToString() == "S")
element1.MergeAttribute("checked", "checked");
TagBuilder element2 = new TagBuilder("input");
element2.MergeAttribute("type", "hidden");
element2.MergeAttribute("name", campo);
element2.MergeAttribute("value", metadata.Model.ToString());
return MvcHtmlString.Create(element1.ToString() + element2.ToString());
I just write a custom mvc helper in asp net mvc 3 and it works.. but it does not look that good:
public static MvcHtmlString BsCheckBox<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
string fieldName = ExpressionHelper.GetExpressionText(expression);
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return MvcHtmlString.Create(
"<input type=\"checkbox\" name=\"" + fieldName+ "_cb\" " + (metadata.Model.ToString() == "S"
? "checked='checked'"
: "") + "/>" +
"<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + metadata.Model.ToString() + "\" />"
);
}
This helper creates two input tag elements to control the checkbox.
"S" == true
"N" == false
There is a better way to write the same thing?
Editing:
The TagBuilder looks a little verbose:
string campo = ExpressionHelper.GetExpressionText(expression);
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
TagBuilder element1 = new TagBuilder("input");
element1.MergeAttribute("type","checkbox");
element1.MergeAttribute("name", campo + "_cb");
if (metadata.Model.ToString() == "S")
element1.MergeAttribute("checked", "checked");
TagBuilder element2 = new TagBuilder("input");
element2.MergeAttribute("type", "hidden");
element2.MergeAttribute("name", campo);
element2.MergeAttribute("value", metadata.Model.ToString());
return MvcHtmlString.Create(element1.ToString() + element2.ToString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 TagBuilder: http:// www.asp.net/mvc/tutorials/using-the-tagbuilder-class-to-build-html-helpers-cs
Use TagBuilder: http://www.asp.net/mvc/tutorials/using-the-tagbuilder-class-to-build-html-helpers-cs