Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use extension method within partial view

I am trying to use a string extension method in a partial view. I get the following error:

'string' does not contain a definition for 'TruncateAtCharacter'

Here is the extension method:

namespace PCCMS.Core.Libraries {
    public static class Extensions {
        public static string TruncateAtCharacter(this string input, int length) {
            if (String.IsNullOrEmpty(input) || input.Length < length)
                return input;

            return string.Format("{0}...", input.Substring(0, length).Trim());
        }
    }
}

According to this previous question I need to add the namespace to web.config, however I have done this and I still receive the same error message. What's odd though, is that I do get intellisense for the extension method?

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="PCCMS.Core.Libraries.ClientWebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <!-- Other namespaces... -->
            <add namespace="PCCMS.Core.Libraries" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

Can anyone explain why this is?

Thanks

like image 393
Rory McCrossan Avatar asked Jul 17 '12 14:07

Rory McCrossan


People also ask

How to use partial view in mvc core?

Declare partial views cshtml markup file without an @page directive maintained within the Views folder (MVC) or Pages folder (Razor Pages). In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view.

What is the difference between view and partial view in mvc?

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

How to render partial view in ASP net core in view?

Partial views are rendered in views using @Html. Partial() method, passing in the name of partial view and optionally a model. Partial view name can be absolute or relative path and view engine will search the current folder or shared folder. Partial View gets a copy of parent view's ViewData dictionary.

How to create partial view in. net core?

The method name contains Async will be rendered for the asynchronous code. The Render methods result need to be written directly to the response. If we want to Create Partial View, then we need to right click on the view folder > click on the folder > select Add > click on View.


1 Answers

This should work if the namespace declaration is in the system.web.webPages.razor/namespaces element of your root Views directory web.config. If that fails, try using an explicit @using statement at the top of the View without any web.config statements. It 'should' work.

PS Is that ReSharper intellisense or VS? ReSharper explicitly tells me that an @using is required if the web.config entry is not in scope.

like image 64
Rob Kent Avatar answered Sep 22 '22 00:09

Rob Kent