ASP.NET MVC 3 multiple Models to single Form

守給你的承諾、 提交于 2019-11-30 13:27:08

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);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!