0

I followed this tutorial to add & remove multiple textbox: http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html. I want to know how to use multiple templates for "EditorFor".

In my Chemical model:

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

In my Chemical controller:

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

In my 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>

and I have a template: 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>

The problem is how can I use another template which include the following code for type 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>

Now these two EditorFor will all use the first template.

Layla Wang
  • 65
  • 7
  • You cannot use a `EditorTemplate` to dynamically add new items to a collection. Refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options –  Dec 07 '15 at 20:35
  • @StephenMuecke I have no problem to use EditorTemplate to add new item based on the first template. The only problem is how to use the 2nd one for the 2nd EditorFor. – Layla Wang Dec 07 '15 at 21:18
  • It is not possible to use an `EditorTemplate` to dynamically add new items and I don't understand why you think you can. Your `Create.cshtml` view is generating 2 identical templates with identical `name` attributes which cannot bind to a collection. –  Dec 07 '15 at 21:22
  • @StephenMuecke check the "HtmlHelpers" in http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add_12.html – Layla Wang Dec 07 '15 at 22:06
  • Thanks, but I do not need to look at bad code :) –  Dec 07 '15 at 22:07

1 Answers1

-1

I believe what you want is a partial view.