简体   繁体   English

传递带有两个类的模型以使用EF5在asp.net中进行查看

[英]Passing a model with two classes to view in asp.net with EF5

I am new to ASP.NET and EF5. 我是ASP.NET和EF5的新手。 I am doing a project in VS2012, MVC4 using EF5. 我正在使用EF5在VS2012,MVC4中进行项目。

I have a class CreateClient where there is further two classes Clients, FullPackages like below: 我有一个类CreateClient那里是进一步两类Clients, FullPackages象下面这样:

public class CreateClient
{
    public Clients Clients { get; set; }
    public List<FullPackages> FullPackages { get; set; }
}

Class Clients is like this: Clients是这样的:

[Table("tblClients")]
public class Clients
{
    public int Id { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Maximum {1} characters allowed for {0} field")]
    public string Username { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Minimum {2} characters required for {0} field", MinimumLength = 5)]
    public string Password { get; set; }

    [Required]
    public string Name { get; set; }

    public string Email { get; set; }
    public string Country { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string Contact { get; set; }

    [Required]
    public string Mobile { get; set; }

    public string Description { get; set; }
}

and Class FullPackages is: FullPackages类是:

public class FullPackages
{
    public int Id { get; set; }
    public int TypeId { get; set; }

    [Display(Name = "Package Type")]
    public string TypeName { get; set; } 
    public int AllowedSMS { get; set; }

    [Display(Name = "Time Span in Days")]
    public int? TimeSpan { get; set; }
    public decimal Price { get; set; }

    [Display(Name = "Package Name")]
    public string Name { get; set; }
}

I invoked this model in my view like 我在我看来调用了这个模型

@model SmsServer.Models.CreateClient

and used the Clients class as 并使用Clients类作为

@Html.LabelFor(model => model.Clients.Username)

but i don't know to use the List<FullPackages> i want to use the values of list. 但是我不知道要使用List<FullPackages>我想使用list的值。 I researched the site but didn't find any useful ideas.So Please help me.... on how to use it 我研究了该网站,但没有找到任何有用的想法。因此,请帮助我...如何使用它

OR is there any way to do this in a more simpler way :) 或者有什么方法可以更简单地做到这一点:)

If you simply want to list them out you could do this: 如果只想将它们列出来,则可以执行以下操作:

<table>
    <tr>
        <th>Id</th>
        <th>TypeName</th>
    </tr>
@foreach(var package in Model.FullPackages)
{
    <tr>
        <td>@package.Id</td>
        <td>@package.TypeName</td>
    </tr>
}
</table>

Please note that if you want to post them back you will have to use an indexed loop instead of the foreach. 请注意,如果要回发它们,则必须使用索引循环而不是foreach。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM