0

I have a fairly simple blazor server side app and am writing data to my db based on drag/drop. I have my functions working to get the id/type at dragstart and the same at drop however the data I store from the dragstart function reverts/dissappears when I start my drop function. What am I doing wrong?

dragged Element

<table class="table AssignmentsTable" draggable="true"
@ondragstart="@(e => AssignmentOnDragStart(item.Id, "Location"))"
@ondragstart:stopPropagation>
    <tr>
        <td>
            <span>@item?.Description</span>
        </td>
    </tr>
</table>

dropped Element

<h5 @ondrop="@(e => AssignmentOnDrop((ParentID ?? 0), "Company"))" ondragover="event.preventDefault()">@company.Name</h5>

.razor (code)

string storedAssigneeType;
int storedAssigneeID;
string storedAssignmentType;
int storedAssignmentID;


public async void AssignmentOnDragStart(int id, string type)
{
    await JSRuntime.InvokeVoidAsync("console.log", $"Before: {type}: {id.ToString()}");
    storedAssigneeID = id;
    storedAssigneeType = type;
    await JSRuntime.InvokeVoidAsync("console.log", $"After: {storedAssigneeType}: {storedAssigneeID.ToString()}");
}

public async void AssignmentOnDrop(int id, string type)
{
    await JSRuntime.InvokeVoidAsync("console.log", $"Stored Assignee: {storedAssigneeType}: {storedAssigneeID.ToString()}");
    await JSRuntime.InvokeVoidAsync("console.log", $"{type}: {id.ToString()}");
    storedAssignmentID = id;
    storedAssignmentType = type;
    var result = @Service.AddAssignment(storedAssigneeType, storedAssigneeID, storedAssignmentType, storedAssignmentID);
    await JSRuntime.InvokeVoidAsync("console.log", $"{result.ToString()}");

}

I'm new so I've tried a lot of the wrong things. I tried {get;set;} for the variables, public/private, and some other items. I know I am just doing something dumb but am at a loss (or its too late at night). There were 2 other questions on the stack that seemed very similar but neither produced a working solution. Help please and thanks.

2 Answers 2

0

Don't use async void, use async Task. Your code should look like this:

public async Task AssignmentOnDragStart(int id, string type)

public async Task AssignmentOnDrop(int id, string type)

@ondragstart="@(async(e) => await AssignmentOnDragStart(item.Id, "Location"))"

@ondrop="@(async(e) => await AssignmentOnDrop((ParentID ?? 0), "Company"))" 

Also, don't use console logging for this. You can do a simple Console.WriteLine and check the output on the process console. That will enable you to write synchronous methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help Mayur. What you posted didn't solve my problem but thank all the same. I have posted a link to the question that ended up getting me where I needed. Sleep and a Monster or two always seem to help.
0

After screwing around for quite awhile I ended up getting it to work with question 53913396. I had tried some similar items from the MS documentation but got a few minor things wrong. Thanks all.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.