简体   繁体   中英

c# mvc only one user to edit tree view at one time

Today, i have a tree view for users to edit it, but i want to lock the action to prevent that A user submit the edited node after B user already deleted the A user's edit node;

I have searched the approach about this scenario, and i prefer to do it at the action filter, if one user gets in this page then set a flag to record, and after the user leaves this page, release the flag;

But i needs help about: (1)how to set the flag when user entering the page by action filter, (2)how to catch the correct timing when user leaves the page and inform the action filter to remove the tag by javascript or razor helper, (3)during the locked period, throw what exception to the other user on the page?

If I were you I would do something like this (I am testing my skills to add meaningful comment rather than describing the code :))

public EditTree()
{
    // Get current logged in user, check if user has permission

    lock(_treeLock)
    {
        // Check if tree can be edited at this moment
        if(!isTreeEditable())
        {
            throw new InvalidOperationException();

            //Or

            return Json("ERROR"); // Display an error on the client as you wish.
        }
        else
        {
            // Set the flag in DB so then no one else can edit

            // Return the tree data for edition
        }
    }
}

If you want to allow user to edit the tree or view the changes in real time, making that possible involves handling a lot many cases (broken connection, DB failure, etc). I suggest you carefully review your requirement and design for simplicity.

To sync the tree for all user in real time you need to use a persistent connection between server and all clients (use SignalR).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM