Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to handle a array of objects in a session
    text
    copied!<p>In the project I'm working on I have got a list <code>List&lt;Item&gt;</code> with objects that Is saved in a session. <code>Session.Add("SessionName", List);</code></p> <p>In the Controller I build a viewModel with the data from this session</p> <pre><code>var arrayList = (List&lt;Item&gt;)Session["SessionName"]; var arrayListItems= new List&lt;CartItem&gt;(); foreach (var item in arrayList) { var listItem = new Item { Amount = item.Amount, Variant= item.variant, Id = item.Id }; arrayListItems.Add(listItem); } var viewModel = new DetailViewModel { itemList = arrayListItems } </code></pre> <p>and in my View I loop trough the list of Items and make a form for all of them to be able to remove the item.</p> <pre><code>&lt;table&gt; &lt;%foreach (var Item in Model.itemList) { %&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;tr&gt; &lt;td&gt;&lt;%=Html.Hidden(Settings.Prefix + ".VariantId", Item .Variant.Id)%&gt; &lt;td&gt; &lt;%=Html.TextBox(Settings.Prefix + ".Amount", Item.Amount)%&gt;&lt;/td&gt; &lt;td&gt; &lt;%=Html.Encode(Item.Amount)%&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="submit" value="Remove" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;% } %&gt; &lt;/table&gt; </code></pre> <p>When the post from the submit button is handeld the item is removed from the array and post back exactly the same viewModel (with 1 item less in the itemList). </p> <pre><code>return View("view.ascx", viewModel); </code></pre> <p>When the post is handled and the view has reloaded the value's of the html.Hidden and Html.Textbox are the value's of the removed item. The value of the html.Encode is the correct value. When i reload the page the correct values are in the fields. Both times i build the viewModel the exact same way. </p> <p>I cant find the cause or solution of this error. I would be very happy with any help to solve this problem</p> <p>Thanx in advance for any tips and help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload