Access HttpContext in ASP.NET Core
-
Thanks, I corrected error in my custom helper, but when I call the helper from views as following: @HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" }) and got following error: Error CS0411 The type arguments for method 'HtmlHelperExt.CHGridTable(HtmlHelper, DataTable, object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. The call on MVC5 works file, How can I use the custom helper on view of Core Web Application (MVC6): The custom helper code as following: public static IHtmlContent CHGridTable(this HtmlHelper htmlHelper, DataTable dt, object htmlAttributes = null) { var urlHelperFactory = (IUrlHelperFactory)htmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelperFactory)); var url = urlHelperFactory.GetUrlHelper(htmlHelper.ViewContext); ...... return new HtmlString(tbBtnAdd.ToString() + tbGridTable.ToString() + script.ToString()); }
JimFeng wrote:
@HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })
That should probably be:
@Html.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })
You need to call it as an extension method on the
IHtmlHelper
instance, not call the static method directly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
JimFeng wrote:
@HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })
That should probably be:
@Html.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })
You need to call it as an extension method on the
IHtmlHelper
instance, not call the static method directly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:
@addTagHelper *, YourAssemblyNameHere
Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:
@addTagHelper *, YourAssemblyNameHere
Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I added @addTagHelper *, HtmlHelperExt in _ViewImports.cshtml file but still got error: Error: CS1061 'IHtmlHelper' does not contain a definition for 'CHDropdownMenu' and no accessible extension method 'CHDropdownMenu' accepting a first argument of type 'IHtmlHelper' could be found (are you missing a using directive or an assembly reference?) on the View page like this (the same as on MVC 5): @model System.Data.DataSet @using HtmlHelperExt; ...... @Html.CHDropdownMenu(Model, "mnuSample1", new { MenuOpenType = "Hover" }) ...... My Custom helper like this: namespace HtmlHelperExt { public static class HtmlHelperExt { public static IHtmlContent CHDropdownMenu(this HtmlHelper htmlHelper, DataSet ds, string divID, object htmlAttributes = null) { ...... return new HtmlString(tbDiv_Menu.ToString()); } } }
-
I added @addTagHelper *, HtmlHelperExt in _ViewImports.cshtml file but still got error: Error: CS1061 'IHtmlHelper' does not contain a definition for 'CHDropdownMenu' and no accessible extension method 'CHDropdownMenu' accepting a first argument of type 'IHtmlHelper' could be found (are you missing a using directive or an assembly reference?) on the View page like this (the same as on MVC 5): @model System.Data.DataSet @using HtmlHelperExt; ...... @Html.CHDropdownMenu(Model, "mnuSample1", new { MenuOpenType = "Hover" }) ...... My Custom helper like this: namespace HtmlHelperExt { public static class HtmlHelperExt { public static IHtmlContent CHDropdownMenu(this HtmlHelper htmlHelper, DataSet ds, string divID, object htmlAttributes = null) { ...... return new HtmlString(tbDiv_Menu.ToString()); } } }
JimFeng wrote:
IHtmlHelper<dataset>' does not contain a definition for 'CHDropdownMenu'
CHDropdownMenu<tmodel>(this HtmlHelper<tmodel> htmlHelper,
You need to declare the helper method as an extension method against the
IHtmlHelper
interface, not theHtmlHelper
class.public static IHtmlContent CHDropdownMenu<TModel>(this IHtmlHelper<TModel> htmlHelper, ...
IHtmlHelper Interface (Microsoft.AspNetCore.Mvc.Rendering) | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:
@addTagHelper *, YourAssemblyNameHere
Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Yes, of course. :doh: You'll want
@using
statements for your namespace, rather than@addTagHelper
statements. Still in the_ViewImports.cshml
file, though.@using HtmlHelperExt
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Yes, of course. :doh: You'll want
@using
statements for your namespace, rather than@addTagHelper
statements. Still in the_ViewImports.cshml
file, though.@using HtmlHelperExt
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Did you change the first parameter of the extension method, as I suggested yesterday?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Did you change the first parameter of the extension method, as I suggested yesterday?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer