ASP.NET MVC 3多个模型,以单一的形式 [英] ASP.NET MVC 3 multiple Models to single Form

查看:83
本文介绍了ASP.NET MVC 3多个模型,以单一的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习ASP.NET MVC 3,并试图建立与由外键链接在一起的多个模型的单一形式的视图。最终的目标是让单一的形式插入所有的数据库表。

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.

问题是,我想不通为什么当我点击鼠标右键,创建窗体未在CSHTML文件自动生成视图。自动生成的code有很大帮助,因为我有一个链接在一起,我必须验证并插入许多表,许多领域。如果它不能自动生成表格,什么是完成这件事的最有效的/优雅的方式?

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.

型号:

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; }
    ...
}

控制器:

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

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

请帮帮忙!

推荐答案

在生成,你创建一个基于CustomerModel一个强类型的视图的视图?如果你再生成视图将不输出页面中的任何值,因为所有的属性对其他对象的引用。您需要包含在模型中的实际值类型脚手架在视图中自动囊括其中。这就是说,你总是可以自行添加视图按照下面的例子。

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.

此外,我注意到你的控制器,你的GET方法不模型返回渲染视图。如果你想有一个查看生成基于该模型,那么你需要传递您希望它产生它的对象。

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

这篇关于ASP.NET MVC 3多个模型,以单一的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆