0

嗨,

与此类绑定模型时遇到问题(以下具体,而不是抽象)。忽略基本属性,它是我有兴趣绑定的列表。

public abstract class MessageModel
{
    public string Tag { get; set; }
    public string Message { get; set; }
    public int Id { get; set; }
    public const int DefaultIdValue = Int32.MinValue;

    public List<LinkModel> Linked { get; set; }
    public List<LinkModel> NotLinked { get; set; }

    protected MessageModel()
    {
        Id = DefaultIdValue;
        Linked = new List<LinkModel>();
        NotLinked = new List<LinkModel>();
    }

    protected MessageModel(string tag, string message):this()
    {
        Tag = tag;
        Message = message;
    }
}

public class TextModel:MessageModel
{
        public int TextId { get; set; }

        public TextModel()
        {
                TextId = DefaultIdValue;
        }
}

这是我在提交时在服务器端收到的提交(为了理智而格式化):

Tag=
&Message=
&NotLinked.index=35fda83a053645e6809bbb8b0ea00103
&NotLinked.index=14c2e286e28b4c9d8f889fb3eb437e5f
&NotLinked.%5b35fda83a053645e6809bbb8b0ea00103%5d.RecipientId=1
&NotLinked.%5b35fda83a053645e6809bbb8b0ea00103%5d.RecipientName=Bob+Biggins
&NotLinked.%5b14c2e286e28b4c9d8f889fb3eb437e5f%5d.RecipientId=2
&NotLinked.%5b14c2e286e28b4c9d8f889fb3eb437e5f%5d.RecipientName=Billy+Oswold
&Submit=Submit

当调用接受该模型的函数时,NotLinked 集合设置为空。丁:

(相关)输出 html 看起来像这样(我试图“假”绑定到:

<ol> and <li> 

用 jQuery 做移动东西的工作)

<div id="NotLinkedContainer">
    <ol id="NotLinked" name="NotLinked" style="width: 500px;height: 200px">
        <li value="1">Bob Biggins
            <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="a0ab331bee2a461084b686e13a87090b" />
            <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientId" name="NotLinked.[a0ab331bee2a461084b686e13a87090b].RecipientId" type="hidden" value="1" />
            <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientName" name="NotLinked.[a0ab331bee2a461084b686e13a87090b].RecipientName" type="hidden" value="Bob Biggins" />
        </li>
        <li value="2">Billy Oswold
            <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="d7d294d3174c4bd98d583e92010359e7" />
            <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientId" name="NotLinked.[d7d294d3174c4bd98d583e92010359e7].RecipientId" type="hidden" value="2" />
            <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientName" name="NotLinked.[d7d294d3174c4bd98d583e92010359e7].RecipientName" type="hidden" value="Billy Oswold" />
        </li>
    </ol>
</div>

有任何想法吗?以前没有做过这种复杂的绑定,所以我对我可能犯的简单错误感到茫然。

4

1 回答 1

1

尝试删除括号前的点符号,因为这不是字典,而是列表,因此您需要使用索引,而不是键。剃刀视图中列表的正确语法应该更像这样:

<div id="NotLinkedContainer">
    <ol id="NotLinked" name="NotLinked" style="width: 500px;height: 200px">
        <li value="1">Bob Biggins
            <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="a0ab331bee2a461084b686e13a87090b" />
            <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientId" name="NotLinked[0].RecipientId" type="hidden" value="1" />
            <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientName" name="NotLinked[0].RecipientName" type="hidden" value="Bob Biggins" />
        </li>
        <li value="2">Billy Oswold
            <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="d7d294d3174c4bd98d583e92010359e7" />
            <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientId" name="NotLinked[1].RecipientId" type="hidden" value="2" />
            <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientName" name="NotLinked[1].RecipientName" type="hidden" value="Billy Oswold" />
        </li>
    </ol>
</div>

这是一篇涵盖您正在尝试做的事情的文章:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

于 2013-01-23T18:49:42.493 回答