ASP.NET MVC:如何使用多个EditorTemplate?

时间:2015-12-07 20:27:38

标签: asp.net asp.net-mvc asp.net-mvc-4

我按照本教程添加&删除多个文本框:http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html。我想知道如何使用多个模板来实现#34; EditorFor"。

在我的化学模型中:

public partial class NPG_Chemical
{

    public NPG_Chemical()
    {
        this.NPG_Chemical_Measurement_Methods = new HashSet<NPG_Chemical_Measurement_Method>();
    }
    public virtual ICollection<NPG_Chemical_Measurement_Method> NPG_Chemical_Measurement_Methods { get; set; }
    internal void CreateMeasurementMethods(int count = 1)
    {
        for (int i = 0; i < count; i++)
        {
            NPG_Chemical_Measurement_Methods.Add(new NPG_Chemical_Measurement_Method());
        }
    }

在我的化学品控制器中:

public ActionResult Create()
    {
        var nPG_Chemical = new NPG_Chemical();
        nPG_Chemical.CreateMeasurementMethods(1);
        return View(nPG_Chemical);
    }

在我的Create.cshtml中:

<div id="type1s">
<label>
    Type1:
</label>
@Html.EditorFor(model => model.NPG_Chemical_Measurement_Methods)
</div>
<div id="type2s">
<label>
    Type2:
</label>
@Html.EditorFor(model => model.NPG_Chemical_Measurement_Methods)
</div>

我有一个模板:NPG_Chemical_Measurement_Method.cshtml

@model NPG_Administrative_Utility.Models.NPG_Chemical_Measurement_Method
<div class="type1" style="display:inline-block;">
<p>type1
    @Html.Hidden("Measurement_Type", "Type1")
    @Html.TextBoxFor(x => x.Measurement_Method)
</p>
</div>

问题是如何使用另一个模板,其中包含类型2 EditorFor的以下代码:

@model NPG_Administrative_Utility.Models.NPG_Chemical_Measurement_Method
<div class="type2" style="display:inline-block;">
<p>type2
    @Html.Hidden("Measurement_Type", "Type2")
    @Html.TextBoxFor(x => x.Measurement_Method)
</p>
</div>

现在这两个EditorFor都将使用第一个模板。

1 个答案:

答案 0 :(得分:-1)

我相信你想要的是局部观点。