如何处理会话中的对象数组
在我正在处理的项目中,我有一个列表 List
,其中包含保存在会话中的对象。 Session.Add("SessionName", List);
在控制器中,我使用此会话中的数据构建一个视图模型
var arrayList = (List<Item>)Session["SessionName"];
var arrayListItems= new List<CartItem>();
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
}
,在我的视图中,我循环遍历项目列表,并为所有项目创建一个表单以便能够删除该项目。
<table>
<%foreach (var Item in Model.itemList) { %>
<% using (Html.BeginForm()) { %>
<tr>
<td><%=Html.Hidden(Settings.Prefix + ".VariantId", Item .Variant.Id)%>
<td> <%=Html.TextBox(Settings.Prefix + ".Amount", Item.Amount)%></td>
<td> <%=Html.Encode(Item.Amount)%> </td>
<td> <input type="submit" value="Remove" /> </td>
</tr>
<% } %>
<% } %>
</table>
当处理来自提交按钮的帖子时,该项目将从数组中删除,并回发完全相同的 viewModel(itemList 中少了 1 个项目)。
return View("view.ascx", viewModel);
当帖子被处理并且视图重新加载时, html.Hidden 和 Html.Textbox 的值是已删除项目的值。 html.Encode 的值是正确的值。当我重新加载页面时,字段中包含正确的值。两次我都以完全相同的方式构建 viewModel 。
我找不到这个错误的原因或解决方案。我将非常高兴能够帮助解决此问题,
提前感谢您提供任何提示和帮助
In the project I'm working on I have got a list List<Item>
with objects that Is saved in a session. Session.Add("SessionName", List);
In the Controller I build a viewModel with the data from this session
var arrayList = (List<Item>)Session["SessionName"];
var arrayListItems= new List<CartItem>();
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
}
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.
<table>
<%foreach (var Item in Model.itemList) { %>
<% using (Html.BeginForm()) { %>
<tr>
<td><%=Html.Hidden(Settings.Prefix + ".VariantId", Item .Variant.Id)%>
<td> <%=Html.TextBox(Settings.Prefix + ".Amount", Item.Amount)%></td>
<td> <%=Html.Encode(Item.Amount)%> </td>
<td> <input type="submit" value="Remove" /> </td>
</tr>
<% } %>
<% } %>
</table>
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).
return View("view.ascx", viewModel);
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.
I cant find the cause or solution of this error. I would be very happy with any help to solve this problem
Thanx in advance for any tips and help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您回发到同一 URL。 ASP.NET 有一个内置机制,可确保表单输入中的值始终返回到浏览器,与回发到同一 URL 时发送的值相同。我同意,这可能是一种非常烦人的行为,但可能在某个地方对其原理进行了长时间的讨论。对于初学者来说,它会破坏 ASP.NET 验证功能上使用相同值的自动填充。
我发现解决这个问题的最简单方法就是发布到不同的 URL 并重定向回来。您还可以通过 AJAX 处理请求来解决该问题。
This happens because you are posting back to the same URL. ASP.NET has a built in mechanism to make sure that values from form inputs are always returned to the browser the same as they are sent in when posting back to the same URL. This can be a very annoying behavior, I agree, but there is likely a lengthy discussion of rationale for it somewhere. For starters, it would break the auto populate with same values on validation feature of ASP.NET.
The easiest way I have found to get past the problem is to just post to a different URL and redirect back. You can also fix the problem by handling the request via AJAX.