Sebastian 506563 Sebastian 506563 - 3 months ago 9
C# Question

How to change method signature/properties to match given execution example?

So I have this:

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Expression<Func<TModel, ControlPermissionType>> mode)
{
MvcHtmlString value = null;
var modeIn = ModelMetadata.FromLambdaExpression(
mode, htmlHelper.ViewData
).Model;
switch ((ControlPermissionType)modeIn)
{
case ControlPermissionType.Read:
value = htmlHelper.TextBoxFor(expression, new { @readonly = "readonly" });
break;
case ControlPermissionType.Edit:
value = htmlHelper.TextBoxFor(expression);
break;
case ControlPermissionType.Deny:
value = new MvcHtmlString(string.Empty);
break;
}
return value;
}


and this is how I am calling it:

@Html.TextBoxFor(a => a.First().BirthDate, a => a.First().Mode)


but what I want is:

@Html.TextBoxFor(a => a.First().BirthDate, a.Mode)


how to do that?

EDIT:

or even
@Html.TextBoxFor(a => a.First().BirthDate)
but in this way how to check if the
a
is implementing interface?

EDIT2:

Answer

@Html.TextBoxFor(a => a.First().BirthDate, a => a.First().Mode)

Need method signature :

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Expression<Func<TModel, ControlPermissionType>> mode)


@Html.TextBoxFor(a => a.First().BirthDate, a.Mode)

Need method signature :

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, ControlPermissionType mode)


@Html.TextBoxFor(a => a.First().BirthDate)

Need method signature :

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)

But in this last case, you miss the ControlPermissionType.

By the way, if you try to call your method with @Html.TextBoxFor(a => a.First().BirthDate), the compiler error should be self explanatory on which signature method is needed.