【发布时间】:2011-05-19 06:47:52
【问题描述】:
我有一个强类型 mvc 页面,我不会将无序列表绑定到对象列表。所以在 mvc 视图中它可能看起来像
<% foreach (var item in Model.WhatYouDoL) { %>
<li><%: Html.Encode(item.Text) %><input type="hidden" name="WhatYouDoL[0].Reference" /></li>
<% } %>
我的视图模型可能看起来像
public class ViewModelQuoteWhatYouDoInMotorTrade
{
public List<WhatYouDo> WhatYouDoL { get; set; }
}
我的列表包含像
这样的对象public struct WhatYouDo
{
public decimal Percent { get; set; }
public string Reference { get; set; }
public string Text { get; set; }
}
如果我将 WhatYouDoL[0].Reference 与索引 ([0]) 一起使用,则此绑定正常,加载时我可以使用索引进行设置。问题是我想在客户端的这个列表中添加和删除。所以我可能有一些 js 添加和额外的列表项并删除当前的。这意味着我必须以某种方式管理名称中的索引,并使它们在客户端保持有序且不重复。有谁知道是否有办法使用名称中的索引。
提前致谢。
【问题讨论】:
-
您想使用 Ajax 更新列表吗?要不然是啥。请提供更多信息。
标签: asp.net-mvc-2 list binding strong-typing