简体   繁体   中英

How split a ASP.Net MVC editor form view file into two or more files?

In our database there is a lot of tables with some commons columns (address columns). I would like to do not copy and paste several times the common columns part. I would like to split the form into 2 files, the main one and common template one. Then use the common one into another views.

Please note that the common columns and other columns is in the same class, the common is not another class. I'm not able to use Editor Templates for it.

for instance, take this:

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

and make it into these files: main view

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

A common form part

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

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

I think what you'll need to do is use the view model pattern. you can have parent and child view models. The parent can accept multiple types of models from the DB in an over loaded constructor. Now you can make an editor template for your child view model.

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

}

And here are your views: one is the parent view

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

And This one goes in the editorTemplates

@model MvcApplication1.Child

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM