Boo
Boo

Reputation: 1644

Again about custom templates

Some times ago I asked about problem with custom template. I find solution to use not-strongly typed view for custom template (for double typed properties):

@{
   string id = ViewData.TemplateInfo.GetFullHtmlFieldId("");
   string name = ViewData.TemplateInfo.GetFullHtmlFieldName("");
   string value = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}", ViewData.ModelMetadata.Model);
} 
<input type="text" id="@id" name="@name" value="@value" />

But I can't find information about how method from TemplateInfo gets the name and id? What parametr I should pass to them, if property is complex-type object?

Upvotes: 0

Views: 514

Answers (1)

Mrchief
Mrchief

Reputation: 76218

They default to HtmlFieldPrefix

public string GetFullHtmlFieldName(string partialFieldName)
{
    return (this.HtmlFieldPrefix + "." + (partialFieldName ?? string.Empty)).Trim(new char[] { '.' });
}


public string GetFullHtmlFieldId(string partialFieldName)
{
    return HtmlHelper.GenerateIdFromName(this.GetFullHtmlFieldName(partialFieldName));
}

Inside your partial view, you can even set the HtmlFieldPrefix to something from your view model

ViewData.TemplateInfo.HtmlFieldPrefix = Model.MyPrefix; // MOdel refers to your view model instance

On a related note, take a look here: http://btburnett.com/2011/03/correcting-mvc-3-editorfor-template-field-names-when-using-collections.html

Upvotes: 1

Related Questions