[英]How do I restrict the values of a parameter for this HTML helper method?
I made a html helper for the facebook like button. 我为facebook喜欢按钮做了一个html助手。
public static MvcHtmlString FacebookLike(this HtmlHelper helper, bool send, bool showFaces, string layout, string href = null, int width = 450)
{
var div = new TagBuilder("div");
div.AddCssClass("fb-like");
div.MergeAttribute("data-send", send.ToString());
div.MergeAttribute("data-width", width.ToString());
div.MergeAttribute("data-show-faces", showFaces.ToString());
div.MergeAttribute("data-layout", layout);
if (href != null)
{
div.MergeAttribute("data-href", href);
}
return new MvcHtmlString(div.ToString());
}
The layout parameter only has 3 possible values (standard, button_count and box_count). layout参数只有3个可能的值(standard,button_count和box_count)。 My question is how can I make this only accept those 3 values?
我的问题是我怎样才能让这只接受这3个值?
Thanks for any help you can give. 谢谢你提供的所有帮助。
Make it an enum
: 把它作为
enum
:
public enum LayoutType
{
standard,
button_count,
box_count
}
and then change your method a bit: 然后稍微改变你的方法:
public static MvcHtmlString FacebookLike(this HtmlHelper helper, bool send, bool showFaces, LayoutType layout, string href = null, int width = 450)
{
var div = new TagBuilder("div");
div.AddCssClass("fb-like");
div.MergeAttribute("data-send", send.ToString());
div.MergeAttribute("data-width", width.ToString());
div.MergeAttribute("data-show-faces", showFaces.ToString());
div.MergeAttribute("data-layout", Enum.GetName(typeof(LayoutType), layout));
if (href != null)
{
div.MergeAttribute("data-href", href);
}
return new MvcHtmlString(div.ToString());
}
note the signature now uses the enum
and the data-layout
is set by grabbing the name of the enum
passed in. 请注意,签名现在使用
enum
并通过获取传入的enum
的名称来设置data-layout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.