将<int>列为实体模型类数据成员+赢得显示

时间:2015-06-23 00:31:07

标签: c# html asp.net entity-framework azure

我有一个Azure数据库的实体类模型。我希望其中一个数据成员是List或类似的东西。基本上我希望能够在实体中存储可变数量的&lt; int&#39; int。到目前为止它编译但它没有显示列表信息。这可能吗?有没有更好的方法在实体类模型中存储可变大小的容器?

我的实体类

public class Attribute
{
    [ScaffoldColumn(false)]
    public int AttributeID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string AttributeName { get; set; }

    [Required, StringLength(10000), Display(Name = "Attribute Description"), DataType(DataType.MultilineText)]
    public string Description { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "F_in")]
    public int F_in { get; set; }

    [Display(Name = "F_out")]
    public int F_out { get; set; }

    [Display(Name = "F_sum")]
    public int F_sum { get; set; }

    public int? CategoryID { get; set; }

    public string CategoryName { get; set; }

    public virtual Category Category { get; set; }

    public List<int> R1 { get; set; }

}

我的标记

<asp:FormView ID="AttributeDetail" runat="server" ItemType="Trojan.Models.Attribute" SelectMethod ="GetAttribute" RenderOuterTable="false">
    <ItemTemplate>
        <div>
            <h1><%#:Item.AttributeName %></h1>
        </div>
        <br />
        <table>
            <tr>
                <td>
                    <img src="/Catalog/Images/<%#:Item.ImagePath %>" style="border:solid; height:300px" alt="<%#:Item.AttributeName %>"/>
                </td>
                <td>&nbsp;</td>  
                <td style="vertical-align: top; text-align:left;">
                    <b>Description:</b><br /><%#:Item.Description %>
                    <br />
                    <span><b>F_in:</b>&nbsp;<%#: String.Format("{0:d}", Item.F_in) %></span>
                    <span><b>F_out:</b>&nbsp;<%#: String.Format("{0:d}", Item.F_out) %></span>
                    <br />
                    <span><b>Attribute Number:</b>&nbsp;<%#:Item.AttributeID %></span>
                    <br />
                    <span><b>R1 Relations: </b>&nbsp;<%#:Item.R1 %></span>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:FormView>

我的代码

public partial class AttributeDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public IQueryable<Trojan.Models.Attribute> GetAttribute(
                        [QueryString("AttributeID")] int? AttributeId,
                        [RouteData] string AttributeName)
    {
        var _db = new Trojan.Models.AttributeContext();
        IQueryable<Trojan.Models.Attribute> query = _db.Attributes;
        if (AttributeId.HasValue && AttributeId > 0)
        {
            query = query.Where(p => p.AttributeID == AttributeId);
        }
        else if (!String.IsNullOrEmpty(AttributeName))
        {
            query = query.Where(p =>
                      String.Compare(p.AttributeName, AttributeName) == 0);
        }
        else
        {
            query = null;
        }
        return query;
    }
}

修改

我忘了在我设置列表的地方添加databaseinitializer

private static List<Attribute> GetAttributes()
    {
        var Attributes = new List<Attribute> {
            new Attribute
            {
                AttributeID = 1,
                AttributeName = "Specification",
                Description = "Insertion",
                ImagePath="one.png",
                F_in = 0,
                F_out = 3,
                CategoryID = 1,
                CategoryName = "Chip Life Cycle",
                R1 = new List<int> {2},
    }

1 个答案:

答案 0 :(得分:0)

这可能不是您预期的答案,但也许它可能是一种解决方法。 改变这个:

<span><b>R1 Relations: </b>&nbsp;<%#:Item.R1 %></span>

到此:

<span><b>R1 Relations: </b>&nbsp;<%#GetList(:Item.R1) %></span>

在您的代码中,添加以下内容:

string delimeter = ",";
var item = items.Select(a => a.ToString()).ToList();
Console.WriteLine(item.Aggregate((i, j) => i.ToString() + delimeter + j.ToString()));

我真的不知道Trojan.Models.Attribute(及其中的语法)。但是,正如您所看到的,逻辑很简单。您只需将其转换为字符串,然后再将其显示为listView成员