none
Cannot call from extension method to another class that uses dependency injection RRS feed

  • Question

  • I have made a HTMLHelper extension method to be used from the Razor view. when this method gets called, some logic is required to get information from some other class/method. This class uses dependency injection.

    Now I'm not sure how can I call that class from the extension method.

    Please see below for the code-

    public static class VehicleTrackingFieldAccessHtmlHelper
    {
    public static IHtmlString VehicleTrackingFieldAccessFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    int VT_ID,
    long OU_ID)
    {
    var x = ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData);
    if (x.AdditionalValues.ContainsKey(CreateNewVehicleTrackingInput.FieldAccessKey))
    {
    var vtField = (Enums.Database.VT_Fields)x.AdditionalValues[CreateNewVehicleTrackingInput.FieldAccessKey];

    // Error/Problem is at this line
    return await new VehicleTrackingFieldAccessHtmlHandler().ProcessFields(vtField, VT_ID, OU_ID);
    }

    return new HtmlString("");
    }
    }

    public class VehicleTrackingFieldAccessHtmlHandler
    {
    private readonly IVehicleTrackingFieldAccessAppService _vehicleTrackingFieldAccessAppService;

    public VehicleTrackingFieldAccessHtmlHandler(IVehicleTrackingFieldAccessAppService vehicleTrackingFieldAccessAppService)
    {
    _vehicleTrackingFieldAccessAppService = vehicleTrackingFieldAccessAppService;
    }

    private async Task<IHtmlString> ProcessFields(Enums.Database.VT_Fields vtField, int VT_ID, long OU_ID)
    {
    var fieldAccess = await GetUserPermissionsForVtFields(VT_ID, OU_ID);
    switch (vtField)
    {
    case Enums.Database.VT_Fields.Customer_First_Name:
    return new HtmlString("");
    default:
    return new HtmlString("");
    }
    }
    }

    Thanks,

    Kamalpreet Singh

    • Moved by CoolDadTx Sunday, September 17, 2017 5:21 AM ASP.NET related
    Sunday, September 17, 2017 2:16 AM

All replies