MVC4 ViewData.TemplateInfo.HtmlFieldPrefix generates an extra point

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().

enter image description here

:

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.

+4
4

HtmlFieldPrefix. MVC , EditorTemplate ( ).

public class ListElement
{
  public string Value { get; set; }
  ....
}

public class MyViewModel
{
  public IEnumerable<ListElement> MyItems { get; set; }
  ....
}

(ListElement.cshtml)

@model YourAssembly.ListElement
@Html.TextBoxFor(m => m.Value)

@model YourAssembly.MyViewModel
...
@Html.EditorFor(m => m.MyItems) // note do not specify the template name

<input type="text" name="MyItems[0].Value" ...>
<input type="text" name="MyItems[1].Value" ...>
....

,

MyPartial.cshtml

@model @model YourAssembly.MyViewModel
@Html.EditorFor(m => m.MyItems)

@Html.Partial("MyPartial")

public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
  Expression<Func<TModel, TProperty>> expression, string partialViewName)
  {
    string name = ExpressionHelper.GetExpressionText(expression);
    object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
    var viewData = new ViewDataDictionary(helper.ViewData)
    {
      TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = name }
    };
    return helper.Partial(partialViewName, model, viewData);
  }
}

@Html.PartialFor(m => m.MyItems, "MyPartial")

@model IEnumerable<YourAssembly.ListElement>
@Html.EditorFor(m => m)
+6

, HtmlFeildPrefix .

, , HtmlFeildPrefix , , .

, .

0
  • :
@Html.Partial("_SeatTypePrices", Model.SeatTypePrices, new ViewDataDictionary
{
    TemplateInfo = new TemplateInfo() {HtmlFieldPrefix = nameof(Model.SeatTypePrices)}
})
  1. :
@model List
@Html.EditorForModel()
  1. :

    @using Cinema.Web.Helpers
    @model Cinema.DataAccess.SectorTypePrice
    @Html.TextBoxFor(x => Model.Price)
    

Thus, your partial view will contain a list of elements with prefixes. Then call EditorForModel()from the folder EditorTemplates.

0
source

If I want to pass HtmlFieldPrefix, I use the following construct:

<div id="_indexmeetpunttoewijzingen">
    @Html.EditorFor(model => model.MyItems, new ViewDataDictionary()
    {
        TemplateInfo = new TemplateInfo()
        {
            HtmlFieldPrefix = "MyItems"
        }
    })
</div>                       
0
source

All Articles