-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class instance as parameter #17210
Comments
Hello @mrlife ... I'll need to do some more work to that one. It came straight over from engineering content, and I didn't have time to do much with it. 🏃 I'll probably work this one with the other one 👉 #17160 ... but it will be perhaps a week or so before I can get to it due to high priority issues that must be resolved first. To make that example work ...
|
@guardrex Thanks! The bit about using |
I'll double check ... I don't recall an error, but it might be there. I'll let u know (and on that issue, too). |
No error here. Console is clean, but it does only work (thus far) with |
To close the loop on this and the attached engineering issue, this is the way it works for me without throwing an error. MyClass.cs
Index.cshtml
Just in case, here are the details for my dotnet install, not sure if different than yours...
|
Ok ... I'll give that a shot here. 🤞 |
That doesn't work here. I've never been able to get the @{
var myObject = new MyClass();
myObject.MyInt = 6;
myObject.MyString = "Set by MyPage";
}
<component type="typeof(MyComponent)" render-mode="Server" param-MyObject="myObject" />
<component type="typeof(MyComponent2)" render-mode="Server" param-MyInt="12345" />
<component type="typeof(MyComponent3)" render-mode="Server" /> ... or with an All three work with @{
var myObject = new MyClass(6, "Set by MyPage");
}
<component type="typeof(MyComponent)" render-mode="ServerPrerendered" param-MyObject="@myObject" />
<component type="typeof(MyComponent2)" render-mode="ServerPrerendered" param-MyInt="12345" />
<component type="typeof(MyComponent3)" render-mode="ServerPrerendered" /> .... soooooooo ... if I don't get clarification, I can only guess at what to say. We might have to wait a week or two for the dust to settle on their current workload. They're all 🏃😅 right now. |
@guardrex it's the missing parameter-less constructor. Do you think we can add a section about this to our docs? If you had Something to the effect of if you see a) If you're running locally, turn on |
So, we've clarified that it's supposed to work with both I've tried what's been suggested regarding the parameterless ctor. No VS output errors (with debug component logging) and no console errors ... but I'm not getting component output. Here's the full set up that I have ... MyClass.cspublic class MyClass
{
public MyClass()
{
}
public int MyInt { get; set; } = 999;
public string MyString { get; set; } = "Initial value";
} Shared/MyComponent.razor<h2>MyComponent</h2>
<p>MyInt: @MyObject.MyInt</p>
<p>MyString: @MyObject.MyString</p>
@code
{
[Parameter]
public MyClass MyObject { get; set; }
} Pages/MyPage.cshtml@page "/MyPage"
@model BlazorServer31.MyPageModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using BlazorServer31
@using BlazorServer31.Shared
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyPage</title>
</head>
<body>
@{
var myObject = new MyClass();
myObject.MyInt = 7;
myObject.MyString = "Set by MyPage";
}
<component type="typeof(MyComponent)" render-mode="Server" param-MyObject="@myObject" />
</body>
</html> What's wrong with that ☝️? Additionally, it's still not clear to me why the parameterless ctor is required in the ... my point is just that the doc can't say "a parameterless ctor is required" when it isn't. I'm missing some understanding. 😕 |
@guardrex Is your result of
|
Same ... I'm on 3.1.102 ... but Windows. |
What happens when you change the class as so: public class MyClass
{
public MyClass(string x)
{
}
public int MyInt { get; set; } = 999;
public string MyString { get; set; } = "Initial value";
} |
Is this with |
No, just
|
It's still misbehaving for Blazor Server with a <html><head>
<meta name="viewport" content="width=device-width">
<title>MyPage</title>
</head>
<body>
<!--Blazor:{"sequence":0,"type":"server","descriptor":"CfDJ8OjqziBPZk1MjaFG6MZVV6W5US\u002Bqz8xZZJdyqSHc3Ym8gB92wvA3xU4jOJVN38wAi1X7Z1lIdg8XDvRzlZlFMgigWNAUxar7rViXiaKs3MpBEs2/CDivdJC9FEMFB5sh85rzjkEsB1UVy/VaWy/6Pamie4edJqKH389gNML201PeeXaE4XAj4qWnUXTm/BYBMl9mBOKjdbOd7LGZANKXsScBeHd2Dmbv/z\u002Bm93WpWo4HT1udMtNarRC9EIrML0pUElE1yvE4twm/dB64hLq8sMZ2sONJ87y1pwSTGmLQij\u002BYH6Yu63cpv\u002BJIK9v9nuGmijrRhULI8YGtNJ/EMCHl6WmAsIbuxjhfaRvsJ7HqMxWShpZnzYZKG66mrpf3RNEmlzTiYKEvP3xD8uIdXWC6OkKEpmxelrJ4\u002BsbeBOkeu22yGbALhoqhi\u002By/Hozi9iM0ija6jkS4XS3cmVDuTl34ln/DpeGCaY/IG1pM8HnkeVyA3MS7ceJ7SHU39f1y8FRKQFtJfV0G1DNTowfV\u002BDXfd6E0umT/HXhJZuQT3fbXefPXdRB5C6NueT3cBOlwmxb\u002B0g=="}-->
</body></html> BUT... let's go ahead and document it for I'll mark the PR as "addressing" this issue to keep it open until we know what's what. EDIT
I'll take a look at that again and report back here. EDIT Ok ... I figured out what I was missing. The page that I created in the test app didn't have the blazor.server.js script! 👦🔫 Ok ... I can get the PR in now. |
[EDIT by guardrex Note to self ... see 👉 https://github.com/dotnet/aspnetcore/issues/19605]
A class instance (that has public properties that are set by a default constructor) as a parameter causes the razor component to not be displayed.
Can the following quote be confirmed?
I checked if it was JSON serializable with the following, and it serializes it as expected. I also included the call to the
<component />
.Code
MyClass
MyComponent
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: