合并视图中的数据输入

本文关键字:数据 输入 视图 合并 | 更新日期: 2023-09-27 18:08:33

我正在ASP中构建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; }
}

合并视图中的数据输入

几个指针,你可能需要更新你的Web_User_Address,排除"WebUsers"和有1个userID,你的web_user Dbset不应该有任何地址链接(或地址ID)可以这样看:一个用户可以有多个地址。即使在你的程序中不是这样。

访问视图模型中的两个dbset,您可以这样做:

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();
    }  
}

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

我重读了你的问题。我终于明白你的意思了。要做到这一点,最简单的方法是用两个dbset创建一个模型:)