MVC4 ViewData.TemplateInfo.HtmlFieldPrefix generates an extra dot

14,659
  1. Call your partial this way:
@Html.Partial("_SeatTypePrices", Model.SeatTypePrices, new ViewDataDictionary
{
    TemplateInfo = new TemplateInfo() {HtmlFieldPrefix = nameof(Model.SeatTypePrices)}
})
  1. Partial view:
@model List
@Html.EditorForModel()
  1. Editor template implementation:

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

This way your partial view will contain list of items with prefixes. And then call EditorForModel() from your EditorTemplates folder.

Share:
14,659
Franva
Author by

Franva

I am a software engineer, working on the .NET platform. I am interested in developing mobile apps, Computer Vision, Image Processing, playing guitar and piano, travelling, cooking etc.

Updated on June 14, 2022

Comments

  • Franva
    Franva almost 2 years

    I'm trying to get name of input correct so a collection of objects on my view model can get 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 pass in the "listName" as the prefix for my template, the value of 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 extra dot. How can I get rid of it?

    Incidentally, I tried to manually specify the name inside the template and found the name gets overridden by the Html helper.

    Update

    The reason why I have to manually set the HtmlFieldPrefix is the property name (MyItems which is a list of objects) will get lost when MyItems is passed from main view to the partial view. By the time, the partial view called my template and passed in one object in MyItems, the template itself has no way to figure out the name of MyItems as it has been lost since the last "pass-in".

    So that's why I have to manually set the html field prefix name. And I even tried to use something similar to reflection(but not reelection, I forgot the name) to check the name of passed in object and found it returned "Model".


    Update 2

    I tried Stephen's approach, and cannot find the html helper PartialFor().

    enter image description here

    I even tried to use this in my main view:

    Html.Partial(Model, "_MyPartialView");
    

    In Partial View:

    @model MvcApplication1.Models.MyModel
    <h2>My Partial View</h2>
    
    
    @Html.EditorFor(m => m.MyProperty)
    

    Here is my templat:

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

    Here is my Model:

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

    And here is my controller:

    public ActionResult MyAction()
    {
        return View(new MyModel());
    }
    

    It only generates raw text("12131415") for me, instead of the wanted text box filled in with 12 13 14 15.

    But if I specified the template name, it then throws an exception saying:

    The template view expecting ListElement, and cannot convert
    List<ListElement> into ListElement.