I'm some what new to Generics and I can't figure out why the following doesn't work.
I have an extension to IEnumerable<T>, called Grid and it looks like so
public static class IEnumberableGridExtension
{
public static HelperResult Grid<T>(this IEnumerable<T> gridItems, Action<GridView<T>> thegrid)
{
........
}
}
Say, I have a variable in my razor Model called "Products" and it is the type List<Product>, so I tried to do
#Model.Products.Grid<Product>(grid=>{
...
});
It states "Cannot convert method group 'Grid" to non-delegate type 'object'. Did you intend to invoke the method?", with "#Model.tasks.Grid" red underlined.
The funny thing is, visual studio compiles, everything is fine. Of course, if i simply do
#Model.Products.Grid(grid=>{
...
});
It's all fine.
The Razor parser is interpreting < and > as normal HTML tags.
You can avoid the problem wrapping the call in parenthesis:
#(Model.Products.Grid<Product>(grid=>{
...
}))
You can find additional info here: How to use generic syntax inside a Razor view file?
Related
I'm trying to build a Proof of Concept with nested components.
I have a main-component which is declared the following
public abstract class VoidHtmlComponent : ComponentBase
{
[Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }
}
and I pass an instance of e.g a TableHead to my Table component
<Shared.RazorComponents.Table TableHead="#(new TableHead{ OnClick = HandleTableHeadClick})" >
</Shared.RazorComponents.Table>
#code{
private void HandleTableHeadClick(MouseEventArgs mouseEventArgs)
{
Trace.WriteLine($"TableHead has been clicked ...");
}
}
This will not build stating
Client\Pages\Table.razor(6,213): Error CS0428: Cannot convert method group 'HandleTableHeadClick' to non-delegate type 'EventCallback'. Did you intend to invoke the method?
I guess blazor is doing some magic here to convert the Action<MouseEventArgs> to an EventCallback<MouseEventArgs> but I am stuck at this point. I also changed the parameter to Action<MouseEventArgs> which throws an error on runtime saying the cast is invalid.
Already tried using EventCallbackFactory but got stuck there as well.
Using OnClick = new EventCallbackFactory().Create(this, HandleTableHeadClick) throws
Function statements require a function name
at runtime.
I think the issue is that you create an instance of the TableHead just like a property.
Since we don't know anything about the Table component it self I just assume that the issue is is around there.
The markup for using Renderfragments for this case would like like something like this:
<Shared.RazorComponents.Table>
<TableHead Context="context">
<p>put what ever you want to render here. Like the nested component with event handler</p>
</TableHead>
</Shared.RazorComponents.Table>
See the docs as a reference.
Starting with an object mapping that was working perfectly fine for SqlLite, I tried to use the same code with Oracle, and found out that, by default, all of my string fields were mapped to VARCHAR2(255). While that works out fine for most of the fields, it's way too small for one field.
I thought it would be a simple matter to make the field bigger. Hah!
I tried this:
Property(prop => prop.BigField, map =>
{
map.Column("BIG_FIELD");
map.Length(65535);
map.Type<NHibernate.Type.StringType>();
});
This produces this error:
NHibernate.MappingException : Could not instantiate IType StringType: System.MissingMethodException: No parameterless constructor defined for this object.
A few of my search results have told me that I should use NHibernateUtil.String instead of NHibernate.Type.StringType. But all that gets me is this:
What's really weird is that I get String as an autocompletion suggestion, but then I can't use it. I tried to see if the code would compile in spite of the red squiggly line, but it doesn't. I also tried StringClob, with the same results.
Any helpful suggestions? All I want to do is make this string bigger than 255 characters. I didn't expect something so simple to fight back so hard.
To use types provided inside NHibernateUtil class use overload taking persistent type as parameter and not inside generic definition:
Property(prop => prop.Property, map =>
{
map.Type(NHibernateUtil.String);
map.Length(length);
});
In you case it seems it makes sense to map it to NHibernateUtil.StringClob:
Property(prop => prop.BigField, map =>
{
map.Column("BigField");
map.Type(NHibernateUtil.StringClob);
});
And there is nothing weird that code is not compiling inside generic for map.Type<NHibernateUtil.String>(); - generic definition expects type (like string) and not instance of type (like "value") which you are trying to supply
This isn't a full answer (so I'm still curious why I had the above problems), but this workaround is good for now:
Property(prop => prop.BigField, map =>
{
map.Column("BigField");
map.Length(65535);
});
The schema shows that the field becomes a CLOB object, and that's just fine for me for this application.
I am new to asp.net core and am trying to render a partial view in an ASP.Net Core application. The address of the partial view is determined at run time. I have constructed a view model which is parsed into the view from the controller and contains the desired file address.
The following code throws the following compiler error:
#Html.Partial(Model.File);
Error CS1973 'IHtmlHelper' has no applicable method named 'Partial'
but appears to have an extension method by that name. Extension methods
cannot be dynamically dispatched. Consider casting the dynamic arguments or
calling the extension method without the extension method syntax.
Any help would be greatly appreciated.
The solution in the end was to specify the type in the Razor file. The following line worked:
#Html.Partial((string) Model.File)
Just to add to Lachlan Fergusson's excellent answer (thank you!) just to say that you also get this message if the name of your view contains a variable, without it's type.
So, the following line threw the error for me:
#Html.Partial("UserDetailsPartial." + language, Model)
...but it went away when I added this...
#Html.Partial("UserDetailsPartial." + (string)language, Model)
Behind the scenes, I had different partials based on language,
UserDetailsPartial.es.html
UserDetailsPartial.de.html
UserDetailsPartial.fr.html
The strange thing is that previously (with an earlier version of .Net Core?) the original line worked fine.
So, add this to Microsoft's "list of error messages which don't really explain what the problem is.."
I am trying to mock up a page using WebMatrix using WebPages under the hood. I have assigned an implicitly typed array of anonymous objects to one of the PageData keys, but I get the following error when I try to using LINQ methods on the collection:
CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Here is some sample code:
#{
PageData["Vals"] = new [] {
new { ID=1, Quantity=5 },
new { ID=2, Quantity=3 }
};
var sum = PageData["Vals"].Sum(x => x.Quantity);
}
If I first store the array in a regular object, I can use the LINQ methods on it just fine. It seems to have a problem when it comes out of PageData as a dynamic object - but I can't quite seem to figure out the secret sauce to coerce it back to the initial type.
The general solution to this problem is to explicitly cast it. i.e. Cast the expression PageData["Vals"] to an array of the type you expect. However, this cannot work with anonymous types because you don't have the handle to its type and therefore cannot cast it.
Once you've stored your new[] { ... } in the dynamically typed PageData, you've lost all compile-time reference to the anonymous type. Therefore, trying to use type-specific LINQ operators on it is a non-starter.
As I mentioned in the comments, the correct solution is to always use strongly-typed models. You should not be relying on anonymous types declared and defined within a view in order to mock up the page. Have the page depend on a real model and populate that model and feed it to the page.
I've been trying to convert my aspx pages to cshtml and having an issue with rendering partial pages from another folder.
What I used to do:
<% Html.RenderPartial("~/Views/Inquiry/InquiryList.ascx", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending));%>
I would think that the equivalent would be:
#Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending))
This is obviously not working, I am getting the following error.
CS1973:
'System.Web.Mvc.HtmlHelper'
has no applicable method named
'Partial' but appears to have an
extension method by that name.
Extension methods cannot be
dynamically dispatched. Consider
casting the dynamic arguments or
calling the extension method without
the extension method syntax.
How would I achieve this with using the Razor view engine?
The RenderPartial does not return a string or IHtmlString value. But does the rendering by calling Write in the Response.
You could use the Partial extension, this returns an MvcHtmlString
#Html.Partial( ....
or
#{ Html.RenderPartial(....); }
If you really want RenderPartial
The compiler cannot choose the correct method because your Model is dynamic. Change the call to:
#Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", (List<string>)Model.InquiryList)
Or to whatever data type InquiryList is.
Remember to include your strongly typed #model directive in your new Razor view. It is an easy step to miss when converting views from .aspx to .cshtml. If you forget, that 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial' error message could appear.