简体   繁体   中英

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

I am new to ASP.NET and EF5. I am doing a project in VS2012, MVC4 using EF5.

I have a class CreateClient where there is further two classes Clients, FullPackages like below:

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

Class Clients is like this:

[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:

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

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

but i don't know to use the List<FullPackages> i want to use the values of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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