ASP.NET MVC 3 multiple Models to single Form

2019-02-09 09:52发布

问题:

I'm learning ASP.NET MVC 3 and trying to create a View with a single form which is made up of multiple Models linked together by a foreign key. The end goal is to have the single form insert into all the database tables.

The problem is that I cannot figure out why when I right click to create the View that the form is not auto-generated in the cshtml file. Auto-generated code helps a lot since I have many tables that link together, and many fields which I must validate and insert. If it's not possible to auto-generate the form, what is the most efficient/elegant way to get this done?

Here's a simplification of what I have.

Model:

class Customer
{
    [Key]
    public UInt64 CustomerId { get; set; }

    [Required(ErrorMessage = "Name is required.")]
    [Display(Name = "Customer Name")]
    [MaxLength(50)]
    public string FullName { get; set; }
}

class CustomerAdditionalDetails1
{
    [ForeignKey("Customer")]
    public UInt64 CustomerId { get; set; }

    [Required(ErrorMessage = "This info is required.")]
    [Display(Name = "Customer Information")]
    [MaxLength(50)]
    public string SomeInfo { get; set; }
}

class CustomerAdditionalDetails2
{  // same foreign key as CustomerAddtionalDetails1, but with different properties
}

class CustomerAdditionalDetails3
{  // same foreign key as CustomerAddtionalDetails1, but with different properties
}

...

public class CustomerModel
{
    public Customer Customer { get; set; }
    public CustomerAdditionalDetails1 CustomerAdditionalDetails1 { get; set; }
    public CustomerAdditionalDetails2 CustomerAdditionalDetails2 { get; set; }
    public CustomerAdditionalDetails3 CustomerAdditionalDetails3 { get; set; }
    ...
}

Controller:

public ActionResult Submit()
{
    return View();
}

[HttpPost]
public ActionResult Submit(CustomerModel customer)
{
    return View();
}

Please help!

回答1:

When you generate the view, are you creating a strongly typed view based on CustomerModel? If you are then generating a view won't output any values in the page because all your properties are references to other objects. You need actual value types contained in the model for the scaffolding to include them in the view automatically. That said you can always add them in the view yourself as per the example below.

Also I notice in your controller that your GET method doesn't return a model to the view to render. If you want to have a view generated based on the model then you need to pass the object that you want it to generate it for.

@model MvcApplication3.Models.CustomerModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<fieldset>
    <legend>CustomerModel</legend>
</fieldset>
<ul>
<li>@Model.Customer.FullName</li>
<li>@Model.CustomerAdditionalDetails1.SomeInfo1</li>
<li>@Model.CustomerAdditionalDetails2.SomeInfo2</li>
</ul>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>


public class CustomerController : Controller
    {
        public ActionResult Index()
        {
            CustomerModel customerModel = new CustomerModel() 
            { 
                Customer = new Customer()
                {
                    FullName = "Dan"
                },
                CustomerAdditionalDetails1 = new CustomerAdditionalDetails1() 
                { 
                    SomeInfo1 = "Somewhere1" 
                },
                CustomerAdditionalDetails2 = new CustomerAdditionalDetails2()
                {
                    SomeInfo2 = "Somewhere2"
                },
                CustomerAdditionalDetails3 = new CustomerAdditionalDetails3()
                {
                    SomeInfo3 = "Somewhere3"
                }
            };

            return View(customerModel);
        }
    }