Answered C# string question

SaeedP

Well-known member
Joined
Oct 21, 2020
Messages
99
Programming Experience
3-5
Hello,

Are the last parts of this code wrong?
I see red lines under some parts.
I doing this project in Blazor.

my code:
  public string resultMessage {get;set;}

var result = await _instaApi.UploadVideoAsync(mediaVideo, mediaImage,VideoUpload.captionVideo);

resultMessage = (result.Succeeded ?$ "Media created: {result.Value.Pk},{result.Value.Caption}"
        : $"Unable to upload video: {reult.Info.Message}");

And then:

C#:
<p>result:@resultMessage</p>

Thanks,

Saeed
 
Solution
One thing you should do, is; change how your code operates. And change your return type and how your calling code receives the result.

C#:
    public async Task StartUploadStory()
You would be better to return your result to the calling code that ran the upload task once it's finished.

I still suggest adding a MVC project to your Blazor app. You won't find many devs who do this, mainly because they don't know that you can use a MVC based web-app to drive your Blazor application. The performance decrease is barely noticeable, and it brings great functionality from using a MVC app. See : Using Blazor Components In An Existing MVC Application I've had more time to look at your code/post.

This : public static string...
If you hover over the red squiggles, or actually compile your code, what is the error message that is returned to you by the compiler?
 
If you hover over the red squiggles, or actually compile your code, what is the error message that is returned to you by the compiler?

Dear skydiver,

I understood my problem, but have another question:
Is this the right way to report the result to the user?
 
It depends on your UI. You said above that this was a Blazor app. I would then assume that you would render the string is a Razor page like you are trying to do above.
 
It depends on your UI. You said above that this was a Blazor app. I would then assume that you would render the string is a Razor page like you are trying to do above.
Can you please explain more?

Blazor will show the result like this:
C#:
<p>result:@resultMessage</p>

I'm doubtful about the C# code:

C#:
  public string resultMessage {get;set;}

var result = await _instaApi.UploadVideoAsync(mediaVideo, mediaImage,VideoUpload.captionVideo);

resultMessage = (result.Succeeded ?$ "Media created: {result.Value.Pk},{result.Value.Caption}"
        : $"Unable to upload video: {reult.Info.Message}");

thanks
 
Your question is terrible and lacks sufficient details to help you. Is this a blazor server app or blazor wasm?

You are supplying a limited amount of code of a basic action without explaining how it operates or how its intended to function.

Where is your code above being executed? Inside a view or a class?

Generally you would return your result to a partial view and render that partial view to your main view. Or you would post your action to a controller, and then submit your model to your view.

Since you are not showing any of this behind the scenes logic, it is hard to answer what we do not know or understand because you have no explained it to us.

As an aside, Blazor is not something I advise running in production. Needless to say myself an @Skydiver have done some testing, and there are many problems within Blazor which still need ironing out. Blazor is also not fully compatible with Bootstrap despite what Microsoft says.

Where does your result get returned. To what type of file?
 
I've moved this thread under "MVC, MVVM, etc."
 
Your question is terrible and lacks sufficient details to help you. Is this a blazor server app or blazor wasm?

You are supplying a limited amount of code of a basic action without explaining how it operates or how its intended to function.

Where is your code above being executed? Inside a view or a class?

Generally you would return your result to a partial view and render that partial view to your main view. Or you would post your action to a controller, and then submit your model to your view.

Since you are not showing any of this behind the scenes logic, it is hard to answer what we do not know or understand because you have no explained it to us.

As an aside, Blazor is not something I advise running in production. Needless to say myself an @Skydiver have done some testing, and there are many problems within Blazor which still need ironing out. Blazor is also not fully compatible with Bootstrap despite what Microsoft says.

Where does your result get returned. To what type of file?

Dear Sheepings:

This is a Blazor server app:

Here is the view:

C#:
@page "/UploadStory"

@using System;
@using System.Collections.Generic;
@using System.IO;
@using System.Threading.Tasks;
@using InstaSharper.API;
@using InstaSharper.API.Builder;
@using InstaSharper.Classes;
@*@using InstaSharper.Examples.Samples;*@
@using InstaSharper.Logger;
@using Microsoft.AspNetCore.Components.Web;
@using Microsoft.JSInterop;
@inject IJSRuntime jsRuntime;
@using InstaSharper.Classes.Models;


<div>
    <label for="story">Story:</label>
</div>

<div>

    <input type="file" id="sUpload" @bind="storyUpload" @bind:event="oninput" />

</div>

<br />
<div>
    link:@storyUpload
</div>

<br/>


<div>
    <label for="caption">Caption:</label>
</div>


<div>

   
    <textarea rows="3" cols="50" name="comment" id="caption" @bind="captionStory" @bind:event="oninput"></textarea>

</div>
<br/>

<div>

     <button type="button" class="btn btn-primary " @onclick="StartUploadStory"  >Submit</button>
</div>
<br/>

<p>result:@resultMessage</p>

And then the code part:

C#:
@code {

    public static string storyUpload { get; set; }
    private readonly IInstaApi _instaApi;
    public static string captionStory { get; set; }
    public string resultMessage {get;set;}



    public async Task StartUploadStory()
    {
        var mediaStory = new InstaImage
        {
            Height = 1080,
            Width = 1080,

         
            URI = new Uri(Path.GetFullPath(UploadStory.storyUpload), UriKind.Absolute).LocalPath
        };

        var result = await _instaApi.UploadStoryPhotoAsync(mediaStory, captionStory);
       
         resultMessage = (result.Succeeded ? $"Media created: {result.Value.pk},{result.Value.Caption}"
        : $"Unable to upload story: {result.Info.Message}");
    }

}
 
This seems to work just fine for me in Blazor:
HTML:
@page "/"

<p>Result: @ResultMessage</p>

<button class="btn btn-primary" @onclick="Roll">Roll Saving Throw</button>


@code {
    static Random _random = new Random();
    public string ResultMessage { get; set; }

    void Roll()
    {
        var randomNumber = _random.Next(20) + 1;
        var failed = randomNumber < 15;
        ResultMessage = failed ? $"Saving throw failed: {randomNumber}."
                                : $"You are saved! You rolled a {randomNumber}!";
    }
}
 
Last edited:
Both.

The pseudo example he posted closely resembles your code minus the await call and random generator. Point is the same. However, may I ask why you chose Blazor?
I'd much rather be using MVC. Using MVC in Blazor isn't required at all. Actually using MVC with Blazor is slightly slower (barely noticeable), but It's also possible, and brings great functionality into your Blazor app. If you are interested in finding out more, google Using blazor components in existing MVC app by Chris Sainty.

Regarding your post :
I'm doubtful about the C# code:
What are you doubtful about and are you experiencing a problem with it?
 
Both.

The pseudo example he posted closely resembles your code minus the await call and random generator. Point is the same. However, may I ask why you chose Blazor?
I'd much rather be using MVC. Using MVC in Blazor isn't required at all. Actually using MVC with Blazor is slightly slower (barely noticeable), but It's also possible, and brings great functionality into your Blazor app. If you are interested in finding out more, google Using blazor components in existing MVC app by Chris Sainty.

Regarding your post :

What are you doubtful about and are you experiencing a problem with it?
I know also WebForms and familliar with MVC.
And why I chose Blazor is I'm very comfortable with this method. The Single Page Application.
 
Well we all have our preferences of what we like and don't like. I like to mix Blazor with MVC personally (When I do use it). Although, I don't use Blazor all that much anymore as I mentioned, that it has a few problems, and Its my own personal opinion that it's not ready to be used on production sites just yet. Microsoft have a terrible habit of releasing products without fully testing them.

Anyway, you never answered me regarding if you are having problems with the code you have. Given you are using Blazor without MVC, I'm not seeing anything that obvious that could be an issue. If there is an issue, do tell.
 
I understood my problem, but have another question:
Then you should have marked this thread Resolved and created a new thread with all and only the information relevant to the new question. One thread pert topic and one topic per thread please, so all conversations are in one place on a single subject.
 
Back
Top Bottom