在视图

时间:2015-08-18 10:35:29

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在ASP.Net中构建一个MVC应用程序,我想将我的用户模型与我的地址模型结合起来进行注册。现在我需要知道如何为此实现ViewModel。我首先使用代码而且我是初学者。

下面是我的两个模型和db上下文的代码。

public class SiteDataModel : DbContext
{
    // Your context has been configured to use a 'SiteDataModel' connection string from your application's 
    // configuration file (App.config or Web.config). By default, this connection string targets the 
    // 'WMVC.SiteDataModel' database on your LocalDb instance. 
    // 
    // If you wish to target a different database and/or database provider, modify the 'SiteDataModel' 
    // connection string in the application configuration file.
    public SiteDataModel()
        : base("name=SiteDataModel")
    {
    }

    // Add a DbSet for each entity type that you want to include in your model. For more information 
    // on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.

    public virtual DbSet<Web_User> WebUsers { get; set; }
    public virtual DbSet<Web_User_Address> WebUserAddresses { get; set; }

}


public class Web_User
{
    [Key]
    public int User_Id { get; set; }

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

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

    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid e-mail address")]
    public string Email { get; set; }

    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "Cellphone Number")]
    [DataType(DataType.PhoneNumber)]
    public string CellPhoneNumber { get; set; }

    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime DateOfBirth { get; set; }

    public int Address_Id { get; set; }
    public virtual Web_User_Address UserAddresses { get; set; }


}

public class Web_User_Address
{
    [Key]
    public int Address_Id { get; set; }

    [Required]
    [Display(Name="Postal Address")]
    [DataType(DataType.PostalCode)]
    public int PostalCode { get; set; }

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

    public string Province { get; set; }

    public ICollection<Web_User> WebUsers { get; set; }
}

2 个答案:

答案 0 :(得分:0)

一些指示,你可能需要更新你的Web_User_Address,以排除“WebUsers”并拥有1个用户ID,而你的web_user Dbset不应该有任何地址链接(或地址ID) 以这种方式看待它:一个用户可以拥有多个地址。即使你的计划不是这样。

在viewmodel中访问两个DbSets,你可以这样做:

class RegistrationViewModel
{
    SiteDataModel db = new SiteDataModel();

    public AddUserAndAddress(Web_User_Address address, Web_User user){
       //Note: address has a userId 
       var user = db.Web_User.First(a => a.Name== user.Name && /*etc*/ );
       if(user != null){
       // User already exists. Do something about that here
       } 

       db.Web_User_Address.Add(address);
       db.Web_User.Add(user);
       db.SaveChanges();
    }  
}

我希望这会有所帮助。祝你好运!

答案 1 :(得分:0)

重新阅读你的问题后。我终于理解了你的要求。 执行此操作的简单方法是使用两个Dbset创建模型:)