asp net mvc 的数据绑定

数据绑定方式1: ViewData.Add("documentRead", new
                    {
                        DocumentID = documentInfo.DocumentID,
                        documentInfo.CategoryID,
                        documentTitle = documentInfo.GetLangInfo(DefaultLanage).DocumentTitle,
                        Description = documentInfo.GetLangInfo(DefaultLanage).Description,
                        Author = documentInfo.GetLangInfo(DefaultLanage).Author,
                        UserID = documentInfo.UserID,
                        Keywords = SearchKeywords(documentInfo.DocumentID),
                        documentURL = GetURL(documentInfo.DocumentID),
                        DownloadCount = documentInfo.DownloadCount,
                    })

这里的code是在控制里面绑定数据,然后可以在aspx页面里面调用documentRead中的DocumentID、CategoryID、documentTitle......等等这些属性,

在aspx页面里面的写法是:<%= ViewData.Eval("documentRead.Keywords")%>,这样就绑定了documentRead中的Keywords的属性

数据绑定方式2:ViewData["dclist"] = listDocument.Select(c => new
                   {
                     c.DocumentID,
                     DocumentName = c.GetLangInfo(DefaultLanage).DocumentTitle,
                     DocumentURL = GetURL(c.DocumentID),
                  })

这里的code是在控制里面绑定数据,然后可以在aspx页面里面调用dclist中的DocumentID、DocumentName、DocumentURL这些属性,

在aspx页面里面的写法是:<%# Eval("DocumentURL")%>,这样就绑定了dclist中的DocumentURL的属性,

上面这两种绑定的方式有点相识,不过要注意区分,各自的功能也不是一样的。

数据绑定方式1是对一个documentInfo对象进行进行绑定的,数据绑定方式2是对一个listDocument列表进行绑定的。

先慢慢体会一下,以后再来更加复杂的绑定。。

原文地址:https://www.cnblogs.com/lmfeng/p/2038158.html