方法重载不确定如何

时间:2013-02-07 13:10:28

标签: c# asp.net-mvc-3 overloading

我有这些方法:

 public static MvcHtmlString GetQTip(this HtmlHelper htmlHelper, string propertyName, string message, string propertyNameOverride = "", QTipPosition position = QTipPosition.Right, bool includeEvents = true, string title = "")
{
  string qtipPosition = String.Empty;

  switch (position)
  {
    case QTipPosition.Right:
      qtipPosition = "my: 'left center', at: 'right center'";
      break;
    case QTipPosition.Left:
      qtipPosition = "my: 'right center', at: 'left center'";
      break;
    case QTipPosition.Top:
      qtipPosition = "my: 'top middle', at: 'bottom middle'";
      break;
    case QTipPosition.Bottom:
      qtipPosition = "my: 'bottom middle', at: 'top middle'";
      break;
  }

  if (!String.IsNullOrWhiteSpace(propertyNameOverride))
    propertyName = propertyNameOverride;

  if (String.IsNullOrWhiteSpace(title))
    title = htmlHelper.Resource(Resources.Global.Title.Information);

  StringBuilder sb = new StringBuilder();
  sb.Append(String.Concat("$('#", propertyName, "').removeData('qtip').qtip({content: {text:"));
  sb.Append(String.Concat("'", message, "', title: { text: '", title, "', button: false }}, position: { ", qtipPosition, " }"));

  if (includeEvents)
    sb.Append(", show: { event: 'focus mouseenter', solo: true, ready: false }, hide: 'blur'");

  sb.Append(", style: { classes: 'ui-tooltip-shadow ui-tooltip-yellow' } });");

  return new MvcHtmlString(sb.ToString());
}

我该如何称呼它:

 <%: Html.GetQTip("training-module-name-container-" + module.CategoryId +"-" + module.TrainingModuleId, trainingModulePointsHintText, Zinc.Web.Extensions.QTipPosition.Bottom, true, Html.Resource(Resources.Training.Module.Completed.View.TrainingModulePoints))%>      

我只想要这些参数 谢谢

1 个答案:

答案 0 :(得分:1)

问题是你不能在中间遗漏一个参数。您可以使用默认参数,但您遗漏的参数必须是列表中的最后一个。有关MSDN的说明,请参阅here

要解决您的问题,您可以选择以下几种方法:

1)使propertyNameOverride参数成为函数定义中的最后一个参数,如下所示:

public static MvcHtmlString GetQTip(this HtmlHelper htmlHelper, string propertyName, string message, QTipPosition position = QTipPosition.Right, bool includeEvents = true, string title = "", string propertyNameOverride = "") { 
    // do stuff
}

然后你可以毫无问题地把它留下来。

2)使用参数,但只需将默认值放在:

<%: Html.GetQTip("training-module-name-container-" + module.CategoryId +"-" + module.TrainingModuleId, trainingModulePointsHintText, "", Zinc.Web.Extensions.QTipPosition.Bottom, true, Html.Resource(Resources.Training.Module.Completed.View.TrainingModulePoints))%>

3)只用你想要的参数写另一个函数,然后用“空”位置的默认参数调用完整函数:

public static MvcHtmlString GetQTip(this HtmlHelper htmlHelper, string propertyName, string message, QTipPosition position = QTipPosition.Right, bool includeEvents = true, string title = "", string propertyNameOverride = "") { 
    return htmlHelper.GetQTip("training-module-name-container-" + module.CategoryId +"-" + module.TrainingModuleId, trainingModulePointsHintText, "", Zinc.Web.Extensions.QTipPosition.Bottom, true, Html.Resource(Resources.Training.Module.Completed.View.TrainingModulePoints))%>
}