ASP.NET MVC3中一个控制器中的多个模型

Got*_*all 1 asp.net-mvc-3-areas asp.net-mvc-3

我有2个模型,ComanyModel和地址模型.在公司模型中,有一个AddressId连接数据库中的两个表.我想注册一个公司并将信息保存在两个单独的表中的数据库中.现在我希望用户在同一页面输入公司信息和地址,但不知道如何去做.我能够在ASP中完成它,但现在我在MVC3中非常新.

请告诉我如何存档这个.

这是我的两个模型类:

公共类CompanyModel

{

    public Guid AddressId { get; set; }
    public string CompanyEmailAddress { get; set; }
    public string DirectorName { get; set; }
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int RegNo { get; set; }
    public string VatNo { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

公共类AddressModel

{

    public string City { get; set; }
    public string Code { get; set; }
    public Guid Id { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Suburb { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Ada*_*men 6

您必须为两个表创建一个viewModel

例如:

public class nameViewModel
{
    public Company Company { get; set; }
    public Address Address { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后在视图中

 @Html.LabelFor(model => model.Company.Name)

 @Html.EditorFor(model => model.Company.Name)
 @Html.ValidationMessageFor(model => model.Company.Name)


 @Html.LabelFor(model => model.Address.Name)

 @Html.EditorFor(model => model.Address.Name)
 @Html.ValidationMessageFor(model => model.Address.Name)
Run Code Online (Sandbox Code Playgroud)