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.