Replies: 7 comments
-
I don't use Razor, so am of little help here. Anyone else had experience? |
Beta Was this translation helpful? Give feedback.
-
Well, I think the question would be the same in Razor pages or Blazor if that makes any difference. Thanks |
Beta Was this translation helpful? Give feedback.
-
I would use a component that has two render fragments and renders one of them depending on the status, something like this: @typeparam T
@Value.Match(x => Some(x), None)
@code {
[Parameter]
public LanguageExt.Option<T> Value { get; set; }
[Parameter]
public RenderFragment<T> Some { get; set; }
[Parameter]
public RenderFragment None { get; set; }
} You then can use it like this: <Option Value="some">
<Some>Hello, @context</Some>
<None>No name given</None>
</Option>
<Option Value="none">
<Some>Hello, @context</Some>
<None>No name given</None>
</Option>
@code
{
public LanguageExt.Option<string> some { get; set; } = "Bob";
public LanguageExt.Option<string> none { get; set; } = LanguageExt.Option<string>.None;
} Of course, a better name should be picked, since it conflicts with |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Sorry for the delay in replying, I never got a notification of your reply. Thanks for the suggestion, it's very neat. The lambda isn't that bad, I've seen worse! One question though. Why did you make it generic? Won't you always be passing in a string? Thanks again |
Beta Was this translation helpful? Give feedback.
-
I actually don't know why I made it generic, I haven't even tested if it works with something like |
Beta Was this translation helpful? Give feedback.
-
OK, as long as I wasn't missing something clever! I'm not really sure what it would mean to render Thanks again for the suggestion, I'll have a play with it. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Your suggestion got me thinking (pretty impressive feat by the way!). First off, I was wrong about the generic constraint. Of course you would want to pass in something other than a string. Suppose you had a page to show customer details, then the type constraint would be
Note that I changed the component name to Second, I previously created a
This of course set me wondering. I have plans for a Thanks again. |
Beta Was this translation helpful? Give feedback.
-
Just trying to get my head around some basic ideas here. Say I have a Razor/Blazor page (same question in both cases), and I want to show details for a customer, based on the ID passed in the URL.
First stab at this would be to do something like this...
That sets the
_customer
correctly, but how do I proceed then? If the customer has been found, I want to show details, if not, I want to show a message.Previously I would have had something like this in my markup...
However, this approach doesn't give me any benefits for using
Option<T>
over a simpleCustomer
variable that can be null if the ID wasn't valid.Using
Match()
on the_customer
variable is awkward in Razor, as it's messy trying to insert markup inside a function...I realise that this is a very simplistic case, and possibly overkill for
Option<T>
, but I'm going one step at a time.Anyone able to advise? Thanks
P.S. I am sure that there are better ways of doing the data access as well, as the code above just uses plain old ASP.NET Core injection. I did that for simplicity, but if can give any guidelines as to how to make that more functional, it would be even better!
Beta Was this translation helpful? Give feedback.
All reactions