I am trying to get the correct input name, so the collection of objects on my view model can be bound.
@{ViewData.TemplateInfo.HtmlFieldPrefix = listName;}
@Html.EditorFor(m => m, "DoubleTemplate", new {
Name = listName,
Index = i,
Switcher = (YearOfProgram >= i +1)
})
As you can see here, I am going to "listName" as a prefix for my template. value listName = "MyItems"
And here is my template:
@model Web.Models.ListElement
@if (ViewData["Switcher"] != null)
{
var IsVisible = (bool)ViewData["Switcher"];
var index = (int)ViewData["Index"];
var thisName = (string)ViewData["Name"] + "[" + index + "].Value";
var thisId = (string)ViewData["Name"] + "_" + index + "__Value";
if (IsVisible)
{
@*<input type="text" value="@Model.Value" name="@thisName" id ="@thisId" class="cell@(index + 1)"/>*@
@Html.TextBoxFor(m => m.Value, new { @class ="cell" + (index + 1)})
@Html.ValidationMessageFor(m => m.Value)
}
}
but I found that the generated name becomes this-> MyItems. [0] .value
It has one additional DOT
Can someone tell me how to get rid of it?
Btw, I tried to manually specify the name inside the template and found that the name was overridden by the Html helper.
So, please help how to get rid of the extra point.
thank
Update
hi @StephenMuecke , HtmlFieldPrefix, - (MyItems, ) , MyItems . , MyItems, MyItems, "". html. - ( , ), , "".
, .
2
html- PartialFor().

:
Html.Partial(Model, "_MyPartialView");
:
@model MvcApplication1.Models.MyModel
<h2>My Partial View</h2>
@Html.EditorFor(m => m.MyProperty)
templat:
@model MvcApplication1.Models.ListElement
@Html.TextBoxFor(m => m.Value)
:
public class MyModel
{
private List<ListElement> myProperty;
public List<ListElement> MyProperty
{
get
{
if (myProperty == null)
{
this.myProperty = new List<ListElement>() { new ListElement() { Value = 12 }, new ListElement() { Value = 13 }, new ListElement() { Value = 14 }, new ListElement() { Value = 15 }, };
}
return this.myProperty;
}
set
{
this.myProperty = value;
}
}
}
public class ListElement
{
[Range(0, 999)]
public double Value { get; set; }
}
:
public ActionResult MyAction()
{
return View(new MyModel());
}
( "12131415" ) , 12 13 14 15
, , , ListElement, List ListElement.