Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:18:58+00:00 2026-06-02T22:18:58+00:00

I seem to have some problems wrapping my head around the concept about how

  • 0

I seem to have some problems wrapping my head around the concept about how to best handle updating and deleting from lists in MVC. I’ll try to explain my problem with a short example. From the Entity Framework I have my object Position:

public int Quantity { get; set; }

public int PositionNumber { get; set; }

public string ArticleNumber { get; set; }

public string Name { get; set; }

public string Remark { get; set; }

Now I’ve heard that it’s best to make a ViewModel foreach View with the used properties so to make it simple the ViewModel looks the same. In the controller we return a list of Positions to the view by mapping the Model to the ViewModel.
Now the view displays textboxes for Quantity and Remark which the user can change. He can also delete a position.
Now when posting the data the user entered I do something like this:

[HttpPost]
    public ActionResult Index(IEnumerable<ShoppingCartPositionModel> shoppingCartPositions)
    {
        var positions = _shoppingCartManager.GetActiveShoppingCart().Positions.Values.OrderBy(p => p.PositionNumber).ToList();

        if (FormCollection.AllKeys.Any(k => k.Contains("Remove")))
        {
            string key = FormCollection.AllKeys.Where(k => k.Contains("Remove")).First();
            int start = key.LastIndexOf('[') + 1;
            int end = key.LastIndexOf(']');
            int difference = end - start;

            int index;
            if (int.TryParse(key.Substring(start, difference), out index))
            {
                _shoppingCartManager.DeleteShoppingCartPosition(positions.ElementAt(index));
            }
        }
        else
        {
            if (ModelState.IsValid)
            {

                for (int i = 0; i < positions.Count(); i++)
                {
                    var position = positions.ElementAt(i);
                    var cartPosition = shoppingCartPositions.ElementAt(i);

                    position.PositionNumber = cartPosition.PositionNumber;
                    position.Quantity = cartPosition.Quantity;
                    position.Remark = cartPosition.Remark;
                }

                _shoppingCartManager.UpdateShoppingCartPositions(positions);
            }
        }

        var cartPositions = GetShoppingCartPositionsModel();
        UpdateModel(cartPositions);

        return View(cartPositions.ToList());
    }

My problem is how will this work if in the meantime another user has deleted an object or even added a new position inbetween the others? Would it be best to make a hidden field on the view and map by that? And to detect updates save a changed date and display a hidden field on the view for that aswell? Because I think if I’d simply check the date update like this I would always have the newest update date and I don’t think in the few seconds the service needs to update an item any other user can change the data.
I hope this explaines my issue and thanks to those trying to help me find a way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:19:00+00:00Added an answer on June 2, 2026 at 10:19 pm

    The first problem you seem to have is that you have different actions on the same form. Would it help if you could handle the submit buttons for delete and update by different actions? There is a nice solution about this here.

    The other point is the detection of possible changes by other users in the meantime. What about a hidden field on the view with the last update time of the whole data structure? If it is still the same on postback, nobody has touched the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some colors that seem to come from a Delphi TColor variable (e.g
I have some troubles while writing logs from log4net to the file. I seem
I seem to be having some problems with something simple here. I have some
I have just deployed my app to heroku, and after some problems, it seem
Have some validating problems which seem to appear only when using the Auth component.
I am very new to MVC3 and having problems wrapping my head around things.
I seem to have some problems installing mod_python with apache. ./configure --prefix=/usr/local/apache2 make make
I am writing my first Android application and have some problems with handing around
So I seem to have some difficulties with making my CodeFile reference into CodeBehind.
I have read through several reviews on Amazon and some books seem outdated. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.