Hello I'm making an MVC with a List<> and not a Database in which I use an ID which is supposed to equal the index in the list. Whenever I want to edit an object and replace the old with the new one the index is constantly changing and I'm not able to successfully replace the object. What am I doing wrong? How can I make a successful EDIT functionality? On the webpage I'm only able to edit the 0 index the first object on the list successfully and all edited objects get 0 index somehow.
Asked
Active
Viewed 55 times
-2
-
how is `HotelBoeking` defined? is it a `class` or a `struct`? – adjan Nov 23 '17 at 20:57
-
It is a class my friend in Models. What's going on is that Im only able to edit the 0th index whenever I click on an object in the list that isn't the 0 I can't edit it. What's wrong about my ID system I just don't know what's going on – John Reagan Nov 23 '17 at 21:00
-
I'm assuming that you want to keep the list sorted by ID? If so, remove the existing entry, add the new one and then sort the list on the ID. You could also use a SortedDictionary. – Kerri Brown Nov 23 '17 at 21:03
-
How can I sort it on ID? – John Reagan Nov 23 '17 at 21:04
-
This answer here (using Linq) looks like just the job https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object – Kerri Brown Nov 23 '17 at 21:06
-
1What you mean index is constantly changing ? Your edit action method code looks fine to me. What specifically is not working and what is your expected behavior ? – Shyju Nov 23 '17 at 21:08
-
Also as you are using a static List, you will have to code for concurrent calls to your controller. – Kerri Brown Nov 23 '17 at 21:13
-
I have changed the EDIT POST method. What's not working now is that\ I'm only able to edit once. Then the Count of the List increases somehow without even adding something. I just don't know what's going on – John Reagan Nov 23 '17 at 21:15
-
Earlier you said you can only edit the 0th index item. Now are you saying the exact opposite ? (You cannot edit the 0th index) ? – Shyju Nov 23 '17 at 21:17
-
My problem in entirety is that I don't know how to use an ID when using a List<> instead of a Database. This is the problem – John Reagan Nov 23 '17 at 21:19
1 Answers
0
In your create method, you are setting the BookingId
of your objects same as your list index. So the only reason you are always updating the first item (0 th index) in the list when the
boeking.BoekingId
expression returns 0
in your HttpPost Edit
method
int i = boekingen2.FindIndex(x => x.BoekingId == boeking.BoekingId);
Why is returning 0
? , It is using the default value of int
. That means your edit form is not sending the value of this property as part of the submitted form data. To fix this, you need to send the value in the request body. You can keep this property value inside a hidden element inside your form.
You can use the Html.HiddenFor
helper method to generate the hidden input.
@model HotelBoeking
@using (Html.BeginForm("Edit", "home"))
{
@Html.HiddenFor(a=>a.BoekingId)
@Html.LabelFor(a=>a.Naam)
@Html.TextBoxFor(a=>a.Naam)
@Html.LabelFor(a=>a.GeboorteJaar)
@Html.TextBoxFor(a=>a.GeboorteJaar)
@Html.LabelFor(a=>a.AantalPersonen)
@Html.TextBoxFor(a=>a.AantalPersonen)
<button type="submit">Save</button>
}

Shyju
- 214,206
- 104
- 411
- 497
-
I just did a DEBUG and the HTML edit form is sending the right IDs. The big problem is in the EDIT POST ACTION method. How can I replace the object and still keep the same index/ID? – John Reagan Nov 23 '17 at 21:28
-
Your current code is updating the item. It looks fine to me. What specific problem are you facing ? – Shyju Nov 23 '17 at 21:30
-
I'm facing the specific problem that I can only edit once. After that the LINQ in the EDIT POST can't find the index of the object. I did a DEBUG and it seems that List count is 3 while there are 2 in the list only. – John Reagan Nov 23 '17 at 21:32
-
The List Count is increasing and when I assign this as ID to a boeking the LINQ is giving me -1 i variable after the second try. List.Remove(i) is giving an error because of the -1 i – John Reagan Nov 23 '17 at 21:33
-
That means, it cannot find the item with that where condition. Check what value are you getting in `boeking.BoekingId` inside your HttpPost edit method. Is that correct one ? Also check the data inside `boekingen` list and see it has corrupted `BookingId` value from your previous attempts. I suggest you delete everything from the static list and try again. – Shyju Nov 23 '17 at 21:34
-
You can use the `boekingen.Clear();` to delete everything from the list. – Shyju Nov 23 '17 at 21:36
-
What are you trying to do. Read the comment carefully. I said , it is possible that you might have some corrput data in your static list from the previous edit attempt. I was suggesting to clear that bad data. you can create an action method called "DeleteAll" , inside which you can do `boekingen.Clear();` to DELETE everything on the list (if you want to clear the data) .You can also trying to reset the app pool to clear it. – Shyju Nov 23 '17 at 21:44
-
The boekingId value in HTTPOST edit method seems to be always ZERO. While when clicking the edit button the right ID is given – John Reagan Nov 23 '17 at 21:48
-
That means you are not sending the correct BookingId from your form. Did you add the hidden input element inside the form as i wrote in the answer ? Check your view source and see you have a hidden element with name `BoekingId` **inside the form** with the correct value – Shyju Nov 23 '17 at 21:49
-
Also if you are always going to keep the Id same as the index, you can do this as well, `boekingen2[boeking.BoekingId] = boeking;` to update the item in the Httppost Edit action method – Shyju Nov 23 '17 at 21:50