ASP.NET MVC 3 多个模型到单个表单
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您生成视图时,您是否基于 CustomerModel 创建强类型视图?如果您生成视图,则不会在页面中输出任何值,因为您的所有属性都是对其他对象的引用。您需要模型中包含实际值类型,以便脚手架将它们自动包含在视图中。也就是说,您始终可以按照下面的示例自行将它们添加到视图中。
我还注意到在您的控制器中,您的 GET 方法不会将模型返回到要渲染的视图。如果您想基于模型生成视图,那么您需要传递您希望为其生成视图的对象。
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.