0

I need to update the HtmlFieldPrefix so that my partial view constructs my fields w/ the appropriate name. However I'm unable to figure out how to pass it in. This is what I've tried, from this link: MVC4 ViewData.TemplateInfo.HtmlFieldPrefix generates an extra dot

@Html.Partial("_SeatTypePrices", Model.SeatTypePrices, new ViewDataDictionary { TemplateInfo = new TemplateInfo() {HtmlFieldPrefix = nameof(Model.SeatTypePrices)} })

However, this doesn't seem to work. In addition, ViewDataDictionary doesn't accept an empty constructor so maybe it's just an outdated answer. I'm using asp.net-core-mvc

misterbee180
  • 325
  • 1
  • 13

1 Answers1

0

I'm adding this question w/ the answer already known. This seems like a pitfall as most answers to this question suggest solutions that really don't work. The solution I tried was what typically comes up when searching for this answer. The reason it doesn't work is because TemplateInfo cannot be assigned to.

To get around this, you need to construct a new instance of ViewData like

var MyPartialViewData= new ViewDataDictionary(ViewData) { { "AnAdditionalViewDataProperty", propertyValue }{...}... };

Then, on the next command set your HtmlFieldPrefix of your ViewData.TemplateInfo

MyPartialViewData.TemplateInfo.HtmlFieldPrefix = "MySpecificFieldName";

Finally pass your constructed View Data into your partial call

<partial name="PartialViewName" model='PartialModel' view-data=MyPartialViewData/>

misterbee180
  • 325
  • 1
  • 13