ASP.NET MVC 3 多个模型到单个表单

发布于 2024-11-09 07:50:20 字数 1518 浏览 2 评论 0原文

我正在学习 ASP.NET MVC 3 并尝试创建一个具有单个表单的视图,该表单由通过外键链接在一起的多个模型组成。最终目标是将单个表单插入到所有数据库表中。

问题是,我无法弄清楚为什么当我右键单击创建视图时,表单没有在 cshtml 文件中自动生成。自动生成的代码很有帮助,因为我有许多链接在一起的表,以及必须验证和插入的许多字段。如果无法自动生成表单,那么最有效/最优雅的方法是什么?

这是我所拥有的内容的简化。

型号:

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

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2024-11-16 07:50:20

当您生成视图时,您是否基于 CustomerModel 创建强类型视图?如果您生成视图,则不会在页面中输出任何值,因为您的所有属性都是对其他对象的引用。您需要模型中包含实际值类型,以便脚手架将它们自动包含在视图中。也就是说,您始终可以按照下面的示例自行将它们添加到视图中。

我还注意到在您的控制器中,您的 GET 方法不会将模型返回到要渲染的视图。如果您想基于模型生成视图,那么您需要传递您希望为其生成视图的对象。

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

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文