如何将ASP.Net MVC编辑器表单视图文件拆分成两个或多个文件?

时间:2016-07-01 02:13:02

标签: asp.net asp.net-mvc view partial-views mvc-editor-templates

在我们的数据库中,有很多表包含一些公共列(地址列)。我想不要复制和粘贴几次常见的列部分。我想将表单拆分为2个文件,主要文件和常用模板文件。然后将常用的一个用于另一个视图。

请注意,公共列和其他列位于同一个类中,常见的不是另一个类。我无法使用编辑器模板。

例如,拿这个:

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

1 个答案:

答案 0 :(得分:1)

我认为您需要做的是使用视图模型模式。您可以拥有父视图模型和子视图模型。父级可以在过载的构造函数中接受来自DB的多种类型的模型。现在,您可以为子视图模型创建一个编辑器模板。

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

这个在editorTemplates

@model MvcApplication1.Child

@Html.LabelFor(model => model.Att2)
@Html.EditorFor(model => model.Att2)

@Html.LabelFor(model => model.Att3)
@Html.EditorFor(model => model.Att3)