简体   繁体   English

ASP.NET 核心 MVC 绑定包含 2 个域的视图模型 Model

[英]ASP.NET Core MVC Bind Viewmodel that contains 2 Domain Model

I am working on a simple Web Application with ASP.NET Core 3.1 MVC Framework and Entity Framework Core.我正在使用 ASP.NET Core 3.1 MVC Framework 和 Entity Framework Core 开发一个简单的 Web 应用程序。

I have 2 domain models: Renter and Address , and a view model RenterViewModel :我有 2 个域模型: RenterAddress ,以及一个视图 model RenterViewModel

public class Renter
{
    public int Id { get; set; }

    [Required]        
    public Type RenterType { get; set;}

    public string CompanyName { get; set;}

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

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

    [Required]
    public Address Address {get; set;}
    
    public int AddressId {get; set;}
}

public class Address
{
    public Address()
    { }
    public int Id { get; set; }        
    
    [Required]
    public string Street { get; set; }
    
    [Required]
    public string StreetNumber { get; set; }
    
    [Required]
    public string PostalCode { get; set; }
   
    [Required]
    public string City { get; set; }
}

public class RenterViewModel
{
    public RenterViewModel(Renter renter, Address address)
    {
        this.Renter = renter;
        this.Address = address;
    }

    public Renter Renter {get; set;}
    public Address Address {get; set;}       
}

For my index view and details view this works fine.对于我的索引视图和详细信息视图,这很好用。

How can I bind this view model in the controller and validate it, in order to store the Address to the database first and then after it the Renter to the database?如何在 controller 中绑定此视图Renter并对其进行验证,以便先将Address存储到数据库,然后将其存储到数据库中?

Renter needs the Id of Address because of a foreign key constraint.由于外键约束, Renter需要Address Id

My RenterController Create method looks like this:我的RenterController Create方法如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,RenterType,CompanyName,FirstName,LastName"), prefix="Renter"] Renter renter,
    [Bind("Street, StreetNumber, City, Postalcode"), prefix="Address"] Address address)
{
        if (ModelState.IsValid)
        {
            _context.Add(address);
            renter.Address_Id = address.id;
            _context.Add(renter);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        return View(new RenterViewModel(renter, address));
}

The problem is that ModelState.IsValid is always false.问题是ModelState.IsValid总是假的。

I created sample and table script for you.我为您创建了示例和表格脚本。 Please check this url https://gofile.io/d/yorkiv请检查此 url https://gofile.io/d/yorkiv

I use scaffold-dbcontext to generate dbcontext and models.我使用scaffold-dbcontext 来生成dbcontext 和模型。

HomeController.cs家庭控制器.cs

[HttpPost]
    public IActionResult Index(Address address, Renter renter)
    {
        using (var context = new TestDBContext())
        {
            address.Renter.Add(renter);
            context.Add(address);
            context.SaveChanges();
        }
        return View();
    }

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

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