8
我正在學習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();
}
請幫幫忙!