Cannot access a #context.field in <SelectedTemplate> in Blazored Typeahead - asp.net-core

I have a problem with Typeahead with my Blazor-Server app:
<BlazoredTypeahead style="width: auto" SearchMethod="SearchUser"
#bind-Value="calc.FkCustomerId">
<SelectedTemplate>
#context.AccountCode
</SelectedTemplate>
<ResultTemplate>
#context.CustomerSname (#context.AccountCode)
</ResultTemplate>
</BlazoredTypeahead>
#{
private async Task<IEnumerable<AutolineAccts>> SearchUser(string SelectedUser)
{
return await Task.FromResult(alContext.AutolineAccts.Where(x => x.CustomerSname.Contains(SelectedUser)).ToList());
}
}
The problem I have occurs in the SelectedTemplate part:
'string' does not contain a definition for 'AccountCode' and no accessible extension method 'AccountCode' accepting a first argument of type 'string' could be found
Intellisense is supposed to show me all the fields of AutolineAccts, but it does not. But it works for the #context object within the node

'string' does not contain a definition for 'AccountCode' and no accessible extension method 'AccountCode' accepting a first argument of type 'string' could be found
The conext in <SelectedTemplate> is corresponding to that in #bind-Value, seems that calc.FkCustomerId is a string type value, and it definetely doesn't have a AccountCode property, so the above error occurs.
You can refer to this article for how to use Blazored Typeahead.

Related

Accessing IsVisible property of a XAML element's parent in code-behind

if (span.Text == "Specific String")
{
var spanAncestor = span.Parent.Parent;
spanAncestor.IsVisible = false; // Throws error. Read below.
}
The error I get is:
Error CS1061 'Element' does not contain a definition for 'IsVisible'
and no accessible extension method 'IsVisible' accepting a first
argument of type 'Element' could be found (are you missing a using
directive or an assembly reference?)
The span has as a parent a FormattedString, which has as a parent a Label.
Is there a way to set IsVisible property for the ancestor element?
Parent is of type Element, which does not have an IsVisible property. You need to cast it first
if (parent is VisibleElement)
{
((VisibleElement)parent).IsVisible = false;
}
Adding to Jason's answer, you could save the typecast, since you're already checking the type in the if-clause and do the following using pattern matching:
if (parent is VisibleElement visibleParent)
{
visibleParent.IsVisible = false;
}

Blazor Reusable RenderFragments in code with event : Cannot convert lambda expression to intended delegate type

Anyone knows why the below doesn't work? If I remove "onclick" event then it compiles & works as expected. Is that we are not allowed to use event within Reusable RenderFragments?
Env: ASP.NET Core 3.1 & Blazor Web Assembly
Thanks a lot for the help in advance!
Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
#code {
RenderFragment<(CategoryDto category, int myId)> rf =
val => __builder =>
{
<h1 #onclick="()=> Console.WriteLine(val.category.Name)">Hello #val.category.Name, #val.myId</h1>
};
}
The #onclick handler in your code will ultimately be created by the EventCallbackFactory. However, that factory needs a reference to 'this' which isn't supported in field initializers - hence you are receiving this error (and probably another one regarding 'this').
The solution is pretty simple. You just need to turn the field into a property like so:
RenderFragment<(CategoryDto category, int myId)> rf => val => __builder =>
{
<h1 #onclick="() => Console.WriteLine(val.category.Name)">Hello #val.category.Name, #val.myId</h1>
};

RazorPages anchor tag helper with multiple parameters

Here's the RazorPages page I'm trying to make a link to:
#page "{ReportId:int}/{SicCode:alpha?}"
This works
<a asp-page="/ReportSics" asp-route-ReportId="3">rs1</a>
it produces
rs1
But this produces a blank href.
<a asp-page="/ReportSics" asp-route-ReportId="3" asp-route-SicCode="10">rss2</a>
That is: the tag helper works with one parameter but not with two.
Why?
Is it possible to make it work?
(I have another page with the same #page but with the second parameter not optional and it appears to be impossible to create a link to it.)
Furthermore, requesting Page/2/M works, but Page/2/12 returns 404. Why? (The second parameter is a string that can sometimes be a number, but it always treated as a string.)
From the learn.microsoft.com webpage asp-all-route-data offers the following:
asp-all-route-data
The asp-all-route-data attribute supports the creation of a dictionary of key-value pairs. The key is the parameter name, and the value is the parameter value.
In the following example, a dictionary is initialized and passed to a Razor view. Alternatively, the data could be passed in with your model.
#{
var parms = new Dictionary<string, string>
{
{ "speakerId", "11" },
{ "currentYear", "true" }
};
}
<a asp-route="speakerevalscurrent"
asp-all-route-data="parms">Speaker Evaluations</a>
The preceding code generates the following HTML:
Speaker Evaluations
Extension: From here the parameters can be accessed either explicitly in the parameter list of the method:
public IActionResult EvaluationCurrent(int speakerId, string currentYear)
or as a collection (See response: queryString:
public IActionResult EvaluationCurrent()
{
var queryString = this.Request.Query;
}
This works
Yes it works because it produces a route that is similar to this baseUrl/reportsics/?reportId=5
And the other produces a URL that is similar to this baseUrl/reportsics/?reportId=5&sicCode=678 and then it doesn't match your route definition. I think you should try this.
Experimental
asp-page="/reportSics/#myId/#sicCode
Though this would not be the right way to do what you're thinking. If you really want to change your URL structure, why not do url-rewrite?
Edit.
Form your recent comments, seems you want to pass many parameters in your action method and not targeting URL structure. Then I recommend you just
public IActionResult(string ReportId, string sicCode)
{
//......
}
//And the your URL target
<a asp-page="ReportSics" asp-route-ReportId="55" asp-route-sicCode="566" ></a>
And then it will match the route. I think you should remove that helper you placed after your #page definition and try it out if this is what you have already done and the problem persists.
It turns out that if a parameter has the constraint :alpha then it only works if the value being passed can not be parsed as an int or float.

How to resolve error in Vuetify rules [v-text-field] & computed property

If I add rules into all my fields -> error rule.
But if I don't add rules on any fields -> no rules error.
My VueAppProject is running ok, but this error on console means something.
Error:
Rules should return a string or boolean, received 'undefined' instead
found in ---> <VTextField>
My guess would be its because you're not qualifying the name of your Rules object - you're asking Vuetify to find a rule called 'required' = but you have nothing in your data() with that name, you have an object called 'mixinsRules' and that in turn has a function called 'required'.
Try qualifying the reference to 'required' with the name of its parent object, like this:
{
field: 'email',
//...
rules: '[mixinsRules.required]'
}

Aurelia Binding errors ( on browser console)

I am using below snippet in html
value.two-way="lstName.IsBlocked ? 'Blocked' : Value2 + ' %'"
on console it shows error as
Uncaught Error: Binding expression
"lstName.IsBlocked?'Blocked':Value2+' %'" cannot be assigned to.
at b.a.assign (aurelia.js?v=1.0009:59)
at a.updateSource (aurelia.js?v=1.0009:61)
at a.call (aurelia.js?v=1.0009:61)
at a.v [as callSubscribers] (aurelia.js?v=1.0009:58)
at a.notify (aurelia.js?v=1.0009:60) `
What can be the reason for this ?
Thanks in advance!
Since you're using a two-way binding, the binding expression needs to be assignable.
As the error says: "lstName.IsBlocked?'Blocked':Value2+' %'" cannot be assigned to.. If your bound property changes from the view, it would have to be able to write back to the expression and update the source value in your view model. You can't really write to an inline if.
Change two-way to to-view and it should work.
EDIT:
Since you need the expression to be writable, you could use a ValueConverter to solve the problem (assuming Value2 is what you want to write to):
export class BlockedValueConverter {
toView(value, isBlocked) {
return isBlocked ? 'Blocked' : value + ' %';
}
fromView(value) {
return value;
}
}
Then in your html (don't forget to require the valueConverter or use globalResources):
value.two-way="Value2 | blocked:lstName.IsBlocked"