如何将 ASP.Net MVC 编辑器表单视图文件拆分为两个或多个文件?
How split a ASP.Net MVC editor form view file into two or more files?
在我们的数据库中有很多表都有一些公共列(地址列)。我不想多次复制和粘贴公共栏部分。我想将表格分成 2 个文件,主要文件和通用模板文件。然后用普通的变成另外一个视图。
请注意,公共栏目和其他栏目在同一个class,公共栏目不是另一个class。我无法为其使用编辑器模板。
例如,拿这个:
@model SGD.Models.Fornecedor
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.att1)
@Html.EditorFor(model => model.att1)
@Html.LabelFor(model => model.att2)
@Html.EditorFor(model => model.att2)
@Html.LabelFor(model => model.att3)
@Html.EditorFor(model => model.att3)
@Html.LabelFor(model => model.att4)
@Html.EditorFor(model => model.att4)
}
并将其放入这些文件中:
主视图
@model SGD.Models.Fornecedor
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.att1)
@Html.EditorFor(model => model.att1)
@// reference to common template
@Html.LabelFor(model => model.att4)
@Html.EditorFor(model => model.att4)
}
普通表单部分
@Html.LabelFor(model => model.att2)
@Html.EditorFor(model => model.att2)
@Html.LabelFor(model => model.att3)
@Html.EditorFor(model => model.att3)
我认为您需要做的是使用视图模型模式。您可以拥有父视图模型和子视图模型。父级可以在重载的构造函数中接受来自数据库的多种类型的模型。现在您可以为您的子视图模型创建一个编辑器模板。
public partial class Company
{
public string Att1 { get; set; }
public string Att2 { get; set; }
public string Att3 { get; set; }
public string Att4 { get; set; }
}
public partial class Company2
{
public string Att1 { get; set; }
public string Att2 { get; set; }
public string Att3 { get; set; }
public string Att4 { get; set; }
}
public class Parent
{
[Display(Name = "*Attribute One")]
[Required(ErrorMessage = "*Required")]
public string Att1 { get; set; }
[Display(Name = "*Attribute Four")]
[Required(ErrorMessage = "*Required")]
public string Att4 { get; set; }
public Child child { get; set; }
public Parent(){}
public Parent(Company company)
{
Att1 = company.Att1;
Att4 = company.Att4;
child = new Child(company.Att2, company.Att3);
}
public Parent(Company2 company)
{
Att1 = company.Att1;
Att4 = company.Att4;
child = new Child(company.Att2, company.Att3);
}
}
public class Child
{
[Display(Name = "*Attribute Two")]
[Required(ErrorMessage = "*Required")]
public string Att2 { get; set; }
[Display(Name = "*Attribute Three")]
[Required(ErrorMessage = "*Required")]
public string Att3 { get; set; }
public Child() { }
public Child(string Att2, String Att3)
{
this.Att2 = Att2;
this.Att3 = Att3;
}
}
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Parent testParent = new Parent(new Company());
return View(testParent);
}
}
以下是您的观点:
一个是父视图
@model MvcApplication1.Parent
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.Att1)<br />
@Html.EditorFor(model => model.Att1)
@Html.EditorFor(model => model.child)<br />
@Html.LabelFor(model => model.Att4)<br />
@Html.EditorFor(model => model.Att4)
}
然后这个进入编辑器模板
@model MvcApplication1.Child
@Html.LabelFor(model => model.Att2)
@Html.EditorFor(model => model.Att2)
@Html.LabelFor(model => model.Att3)
@Html.EditorFor(model => model.Att3)
在我们的数据库中有很多表都有一些公共列(地址列)。我不想多次复制和粘贴公共栏部分。我想将表格分成 2 个文件,主要文件和通用模板文件。然后用普通的变成另外一个视图。
请注意,公共栏目和其他栏目在同一个class,公共栏目不是另一个class。我无法为其使用编辑器模板。
例如,拿这个:
@model SGD.Models.Fornecedor
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.att1)
@Html.EditorFor(model => model.att1)
@Html.LabelFor(model => model.att2)
@Html.EditorFor(model => model.att2)
@Html.LabelFor(model => model.att3)
@Html.EditorFor(model => model.att3)
@Html.LabelFor(model => model.att4)
@Html.EditorFor(model => model.att4)
}
并将其放入这些文件中: 主视图
@model SGD.Models.Fornecedor
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.att1)
@Html.EditorFor(model => model.att1)
@// reference to common template
@Html.LabelFor(model => model.att4)
@Html.EditorFor(model => model.att4)
}
普通表单部分
@Html.LabelFor(model => model.att2)
@Html.EditorFor(model => model.att2)
@Html.LabelFor(model => model.att3)
@Html.EditorFor(model => model.att3)
我认为您需要做的是使用视图模型模式。您可以拥有父视图模型和子视图模型。父级可以在重载的构造函数中接受来自数据库的多种类型的模型。现在您可以为您的子视图模型创建一个编辑器模板。
public partial class Company
{
public string Att1 { get; set; }
public string Att2 { get; set; }
public string Att3 { get; set; }
public string Att4 { get; set; }
}
public partial class Company2
{
public string Att1 { get; set; }
public string Att2 { get; set; }
public string Att3 { get; set; }
public string Att4 { get; set; }
}
public class Parent
{
[Display(Name = "*Attribute One")]
[Required(ErrorMessage = "*Required")]
public string Att1 { get; set; }
[Display(Name = "*Attribute Four")]
[Required(ErrorMessage = "*Required")]
public string Att4 { get; set; }
public Child child { get; set; }
public Parent(){}
public Parent(Company company)
{
Att1 = company.Att1;
Att4 = company.Att4;
child = new Child(company.Att2, company.Att3);
}
public Parent(Company2 company)
{
Att1 = company.Att1;
Att4 = company.Att4;
child = new Child(company.Att2, company.Att3);
}
}
public class Child
{
[Display(Name = "*Attribute Two")]
[Required(ErrorMessage = "*Required")]
public string Att2 { get; set; }
[Display(Name = "*Attribute Three")]
[Required(ErrorMessage = "*Required")]
public string Att3 { get; set; }
public Child() { }
public Child(string Att2, String Att3)
{
this.Att2 = Att2;
this.Att3 = Att3;
}
}
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Parent testParent = new Parent(new Company());
return View(testParent);
}
}
以下是您的观点: 一个是父视图
@model MvcApplication1.Parent
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.Att1)<br />
@Html.EditorFor(model => model.Att1)
@Html.EditorFor(model => model.child)<br />
@Html.LabelFor(model => model.Att4)<br />
@Html.EditorFor(model => model.Att4)
}
然后这个进入编辑器模板
@model MvcApplication1.Child
@Html.LabelFor(model => model.Att2)
@Html.EditorFor(model => model.Att2)
@Html.LabelFor(model => model.Att3)
@Html.EditorFor(model => model.Att3)