简体   繁体   English

如何限制此HTML帮助程序方法的参数值?

[英]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.

相关问题 如何将方法的参数限制为一组特定的值? - How to restrict the parameter of a method to a specific set of values? 在另一个HTML Helper方法中将HTML Helper方法作为参数传递 - Pass Html Helper Method as Parameter in another Html Helper Method 如何限制方法中传递的参数 - How to restrict passed parameter in method 如何将模型值作为javascript函数参数从asp.net mvc Html助手中传递出来,如Html.Radio? - How do I pass model values as javascript function parameters from inside an asp.net mvc Html helper such a Html.Radio? 如何对调用Action的HTML Helper进行单元测试? - How do I unit test a HTML Helper that does a call to an Action? 自定义HTML帮助程序:如何获取lambda表达式的值? - Custom HTML helper : how do I get the value of the lambda expression? 如何限制在webconfig中禁用它的方法调用? - How do I restrict a method call disabling it in webconfig? 如何通过锚标记帮助器传递值数组? - How Do I Pass an Array of Values via Anchor Tag Helper? 我必须为`→`使用@ Html.ActionLink helper方法提供一个向右的尾箭头做什么? - What must I do for `→` to give a trailing right arrow with the @Html.ActionLink helper method? MVC 4-如何将模板传递给html helper方法 - MVC 4 - How to pass a template to an html helper method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM